- Notifications
You must be signed in to change notification settings - Fork 13.2k
Reduce intersections with conflicting privates, elaborate on reasons#37762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
3aa47627bf375f3197c17a3b90b8a4c8b6e887083885db315File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -4148,7 +4148,9 @@ namespace ts{ | ||||||
| return undefined!; // TODO: GH#18217 | ||||||
| } | ||||||
| type = getReducedType(type); | ||||||
| if (!(context.flags & NodeBuilderFlags.NoTypeReduction)){ | ||||||
| type = getReducedType(type); | ||||||
| } | ||||||
| if (type.flags & TypeFlags.Any){ | ||||||
| context.approximateLength += 3; | ||||||
| @@ -8460,17 +8462,20 @@ namespace ts{ | ||||||
| error(baseTypeNode.expression, Diagnostics.No_base_constructor_has_the_specified_number_of_type_arguments); | ||||||
| return type.resolvedBaseTypes = emptyArray; | ||||||
| } | ||||||
| baseType = getReducedType(getReturnTypeOfSignature(constructors[0])); | ||||||
| baseType = getReturnTypeOfSignature(constructors[0]); | ||||||
| } | ||||||
| if (baseType === errorType){ | ||||||
| return type.resolvedBaseTypes = emptyArray; | ||||||
| } | ||||||
| if (!isValidBaseType(baseType)){ | ||||||
| error(baseTypeNode.expression, Diagnostics.Base_constructor_return_type_0_is_not_an_object_type_or_intersection_of_object_types_with_statically_known_members, typeToString(baseType)); | ||||||
| const reducedBaseType = getReducedType(baseType); | ||||||
| if (!isValidBaseType(reducedBaseType)){ | ||||||
| const elaboration = elaborateNeverIntersection(/*errorInfo*/ undefined, baseType); | ||||||
| const diagnostic = chainDiagnosticMessages(elaboration, Diagnostics.Base_constructor_return_type_0_is_not_an_object_type_or_intersection_of_object_types_with_statically_known_members, typeToString(reducedBaseType)); | ||||||
| diagnostics.add(createDiagnosticForNodeFromMessageChain(baseTypeNode.expression, diagnostic)); | ||||||
| return type.resolvedBaseTypes = emptyArray; | ||||||
| } | ||||||
| if (type === baseType || hasBaseType(baseType, type)){ | ||||||
| if (type === reducedBaseType || hasBaseType(reducedBaseType, type)){ | ||||||
| error(type.symbol.valueDeclaration, Diagnostics.Type_0_recursively_references_itself_as_a_base_type, | ||||||
| typeToString(type, /*enclosingDeclaration*/ undefined, TypeFormatFlags.WriteArrayAsGenericType)); | ||||||
| return type.resolvedBaseTypes = emptyArray; | ||||||
| @@ -8482,7 +8487,7 @@ namespace ts{ | ||||||
| // partial instantiation of the members without the base types fully resolved | ||||||
| type.members = undefined; | ||||||
| } | ||||||
| return type.resolvedBaseTypes = [baseType]; | ||||||
| return type.resolvedBaseTypes = [reducedBaseType]; | ||||||
| } | ||||||
| function areAllOuterTypeParametersApplied(type: Type): boolean{// TODO: GH#18217 Shouldn't this take an InterfaceType? | ||||||
| @@ -10457,7 +10462,7 @@ namespace ts{ | ||||||
| else if (type.flags & TypeFlags.Intersection){ | ||||||
| if (!((<IntersectionType>type).objectFlags & ObjectFlags.IsNeverIntersectionComputed)){ | ||||||
| (<IntersectionType>type).objectFlags |= ObjectFlags.IsNeverIntersectionComputed | | ||||||
| (some(getPropertiesOfUnionOrIntersectionType(<IntersectionType>type), isDiscriminantWithNeverType) ? ObjectFlags.IsNeverIntersection : 0); | ||||||
| (some(getPropertiesOfUnionOrIntersectionType(<IntersectionType>type), isNeverReducedProperty) ? ObjectFlags.IsNeverIntersection : 0); | ||||||
| } | ||||||
| return (<IntersectionType>type).objectFlags & ObjectFlags.IsNeverIntersection ? neverType : type; | ||||||
| } | ||||||
| @@ -10476,12 +10481,39 @@ namespace ts{ | ||||||
| return reduced; | ||||||
| } | ||||||
| function isNeverReducedProperty(prop: Symbol){ | ||||||
| return isDiscriminantWithNeverType(prop) || isConflictingPrivateProperty(prop); | ||||||
| } | ||||||
| function isDiscriminantWithNeverType(prop: Symbol){ | ||||||
| // Return true for a synthetic non-optional property with non-uniform types, where at least one is | ||||||
| // a literal type and none is never, that reduces to never. | ||||||
| return !(prop.flags & SymbolFlags.Optional) && | ||||||
| (getCheckFlags(prop) & (CheckFlags.Discriminant | CheckFlags.HasNeverType)) === CheckFlags.Discriminant && | ||||||
| !!(getTypeOfSymbol(prop).flags & TypeFlags.Never); | ||||||
| } | ||||||
| function isConflictingPrivateProperty(prop: Symbol){ | ||||||
| // Return true for a synthetic property with multiple declarations, at least one of which is private. | ||||||
| return !prop.valueDeclaration && !!(getCheckFlags(prop) & CheckFlags.ContainsPrivate); | ||||||
| } | ||||||
| function elaborateNeverIntersection(errorInfo: DiagnosticMessageChain | undefined, type: Type){ | ||||||
| if (getObjectFlags(type) & ObjectFlags.IsNeverIntersection){ | ||||||
| const neverProp = find(getPropertiesOfUnionOrIntersectionType(<IntersectionType>type), isDiscriminantWithNeverType); | ||||||
| if (neverProp){ | ||||||
| return chainDiagnosticMessages(errorInfo, Diagnostics.The_intersection_0_was_reduced_to_never_because_property_1_has_conflicting_types_in_some_constituents, | ||||||
| typeToString(type, /*enclosingDeclaration*/ undefined, TypeFormatFlags.NoTypeReduction), symbolToString(neverProp)); | ||||||
| } | ||||||
| const privateProp = find(getPropertiesOfUnionOrIntersectionType(<IntersectionType>type), isConflictingPrivateProperty); | ||||||
| if (privateProp){ | ||||||
| return chainDiagnosticMessages(errorInfo, Diagnostics.The_intersection_0_was_reduced_to_never_because_property_1_exists_in_multiple_constituents_and_is_private_in_some, | ||||||
| typeToString(type, /*enclosingDeclaration*/ undefined, TypeFormatFlags.NoTypeReduction), symbolToString(privateProp)); | ||||||
| } | ||||||
| } | ||||||
| return errorInfo; | ||||||
| } | ||||||
| /** | ||||||
| * Return the symbol for the property with the given name in the given type. Creates synthetic union properties when | ||||||
| * necessary, maps primitive types and type parameters are to their apparent types, and augments with properties from | ||||||
| @@ -15609,6 +15641,9 @@ namespace ts{ | ||||||
| return result; | ||||||
| } | ||||||
| ||||||
| elseif(target.flags&TypeFlags.Never&&originalTarget.flags&TypeFlags.Intersection){ | |
| elseif(getObjectFlags(originalTarget)&ObjectFlags.IsNeverIntersection){ |
You can also move the check itself into a function that tries to elaborate on the current errorInfo and do an unconditional assignment (implementation was posted in one of my earlier comments.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| tests/cases/compiler/intersectionWithConflictingPrivates.ts(5,4): error TS2339: Property 'y' does not exist on type 'never'. | ||
| The intersection 'A & B' was reduced to 'never' because property 'x' exists in multiple constituents and is private in some. | ||
| tests/cases/compiler/intersectionWithConflictingPrivates.ts(6,1): error TS2322: Type '{}' is not assignable to type 'never'. | ||
| The intersection 'A & B' was reduced to 'never' because property 'x' exists in multiple constituents and is private in some. | ||
| ==== tests/cases/compiler/intersectionWithConflictingPrivates.ts (2 errors) ==== | ||
| class A{private x: unknown; y?: string} | ||
| class B{private x: unknown; y?: string} | ||
| declare let ab: A & B; | ||
| ab.y = 'hello' | ||
| ~ | ||
| !!! error TS2339: Property 'y' does not exist on type 'never'. | ||
| !!! error TS2339: The intersection 'A & B' was reduced to 'never' because property 'x' exists in multiple constituents and is private in some. | ||
| ab ={}; | ||
| ~~ | ||
| !!! error TS2322: Type '{}' is not assignable to type 'never'. | ||
| !!! error TS2322: The intersection 'A & B' was reduced to 'never' because property 'x' exists in multiple constituents and is private in some. | ||
| function f1(node: A | B){ | ||
| if (node instanceof A || node instanceof A){ | ||
| node; // A | ||
| } | ||
| else{ | ||
| node; // B | ||
| } | ||
| node; // A | B | ||
| } | ||
| // Repro from #37659 | ||
| abstract class ViewNode{} | ||
| abstract class ViewRefNode extends ViewNode{} | ||
| abstract class ViewRefFileNode extends ViewRefNode{} | ||
| class CommitFileNode extends ViewRefFileNode{ | ||
| private _id: any; | ||
| } | ||
| class ResultsFileNode extends ViewRefFileNode{ | ||
| private _id: any; | ||
| } | ||
| class StashFileNode extends CommitFileNode{ | ||
| private _id2: any; | ||
| } | ||
| class StatusFileNode extends ViewNode{ | ||
| private _id: any; | ||
| } | ||
| class Foo{ | ||
| private async foo(node: CommitFileNode | ResultsFileNode | StashFileNode){ | ||
| if ( | ||
| !(node instanceof CommitFileNode) && | ||
| !(node instanceof StashFileNode) && | ||
| !(node instanceof ResultsFileNode) | ||
| ){ | ||
| return; | ||
| } | ||
| await this.bar(node); | ||
| } | ||
| private async bar(node: CommitFileNode | ResultsFileNode | StashFileNode | StatusFileNode, options?:{}){ | ||
| return Promise.resolve(undefined); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add related spans pointing at the location(s) of the private declarations? I imagine one of the possible "fixes" for this error is just making the fields public.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could consider it, but not super high value given that this is a pretty esoteric error.