Skip to content
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

Report primitive type in literal-to-primitive relation complaints #38049

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15489,13 +15489,23 @@ namespace ts {
}
}

if (isLiteralType(source) && !forEachType(target, isUnitType)) {
if (isLiteralType(source) && !typeCouldHaveNoTopLevelSingletonTypes(target)) {
generalizedSourceType = getTypeNameForErrorDisplay(getBaseTypeOfLiteralType(source));
}

reportError(message, generalizedSourceType, targetType);
}

function typeCouldHaveNoTopLevelSingletonTypes(type: Type) {
return forEachType(type, typeCouldHaveNoTopLevelSingletonTypesWorker);
}

function typeCouldHaveNoTopLevelSingletonTypesWorker(type: Type): boolean {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I think we're getting there. First, I would move these functions out so that they don't add more to the current closure on every call.

Second, I think we have to go deeper on constraints, and we can get rid of the "Worker" function.

            function typeCouldHaveNoTopLevelSingletonTypes(type: Type): boolean {
                if (type.flags & TypeFlags.UnionOrIntersection) {
                    return !!forEach((type as IntersectionType).types, typeCouldHaveNoTopLevelSingletonTypes);
                }

                if (type.flags & TypeFlags.Instantiable) {
                    const constraint = getConstraintOfType(type);
                    if (constraint) {
                        return typeCouldHaveNoTopLevelSingletonTypes(constraint);
                    }
                }

                return isUnitType(type);
            }

return (type.flags & TypeFlags.Intersection)
? !!forEach((type as IntersectionType).types, typeCouldHaveNoTopLevelSingletonTypesWorker)
: isUnitType(type) || !!(type.flags & TypeFlags.Instantiable);
}

function tryElaborateErrorsForPrimitivesAndObjects(source: Type, target: Type) {
const sourceType = symbolValueDeclarationIsContextSensitive(source.symbol) ? typeToString(source, source.symbol.valueDeclaration) : typeToString(source);
const targetType = symbolValueDeclarationIsContextSensitive(target.symbol) ? typeToString(target, target.symbol.valueDeclaration) : typeToString(target);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ tests/cases/compiler/complicatedIndexedAccessKeyofReliesOnKeyofNeverUpperBound.t
Type '{ type: T; localChannelId: string; }' is not assignable to type 'Pick<ChannelOfType<T, TextChannel> | ChannelOfType<T, EmailChannel>, "type">'.
Types of property 'type' are incompatible.
Type 'T' is not assignable to type 'ChannelOfType<T, TextChannel>["type"] & ChannelOfType<T, EmailChannel>["type"]'.
Type 'string' is not assignable to type 'ChannelOfType<T, TextChannel>["type"] & ChannelOfType<T, EmailChannel>["type"]'.
Type 'string' is not assignable to type 'ChannelOfType<T, TextChannel>["type"] & ChannelOfType<T, EmailChannel>["type"]'.
Type 'string' is not assignable to type 'ChannelOfType<T, TextChannel>["type"]'.
Type '"text" | "email"' is not assignable to type 'ChannelOfType<T, TextChannel>["type"] & ChannelOfType<T, EmailChannel>["type"]'.
Type '"text"' is not assignable to type 'ChannelOfType<T, TextChannel>["type"] & ChannelOfType<T, EmailChannel>["type"]'.
Type '"text"' is not assignable to type 'ChannelOfType<T, TextChannel>["type"]'.
Type 'T' is not assignable to type 'ChannelOfType<T, TextChannel>["type"]'.
Type 'string' is not assignable to type 'ChannelOfType<T, TextChannel>["type"]'.
Type 'string' is not assignable to type 'ChannelOfType<T, TextChannel>["type"]'.
Type 'string' is not assignable to type 'T & "text"'.
Type 'string' is not assignable to type 'T'.
Type '"text" | "email"' is not assignable to type 'ChannelOfType<T, TextChannel>["type"]'.
Type '"text"' is not assignable to type 'ChannelOfType<T, TextChannel>["type"]'.
Type '"text"' is not assignable to type 'T & "text"'.
Type '"text"' is not assignable to type 'T'.
'"text"' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '"text" | "email"'.
Type 'T' is not assignable to type 'T & "text"'.
Type 'string' is not assignable to type 'T & "text"'.
Type 'string' is not assignable to type 'T & "text"'.
Type 'string' is not assignable to type 'T'.
Type '"text" | "email"' is not assignable to type 'T & "text"'.
Type '"text"' is not assignable to type 'T & "text"'.
Type '"text"' is not assignable to type 'T'.
'"text"' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '"text" | "email"'.
Type 'T' is not assignable to type '"text"'.
Type '"text" | "email"' is not assignable to type '"text"'.
Expand Down Expand Up @@ -60,19 +60,19 @@ tests/cases/compiler/complicatedIndexedAccessKeyofReliesOnKeyofNeverUpperBound.t
!!! error TS2322: Type '{ type: T; localChannelId: string; }' is not assignable to type 'Pick<ChannelOfType<T, TextChannel> | ChannelOfType<T, EmailChannel>, "type">'.
!!! error TS2322: Types of property 'type' are incompatible.
!!! error TS2322: Type 'T' is not assignable to type 'ChannelOfType<T, TextChannel>["type"] & ChannelOfType<T, EmailChannel>["type"]'.
!!! error TS2322: Type 'string' is not assignable to type 'ChannelOfType<T, TextChannel>["type"] & ChannelOfType<T, EmailChannel>["type"]'.
!!! error TS2322: Type 'string' is not assignable to type 'ChannelOfType<T, TextChannel>["type"] & ChannelOfType<T, EmailChannel>["type"]'.
!!! error TS2322: Type 'string' is not assignable to type 'ChannelOfType<T, TextChannel>["type"]'.
!!! error TS2322: Type '"text" | "email"' is not assignable to type 'ChannelOfType<T, TextChannel>["type"] & ChannelOfType<T, EmailChannel>["type"]'.
!!! error TS2322: Type '"text"' is not assignable to type 'ChannelOfType<T, TextChannel>["type"] & ChannelOfType<T, EmailChannel>["type"]'.
!!! error TS2322: Type '"text"' is not assignable to type 'ChannelOfType<T, TextChannel>["type"]'.
!!! error TS2322: Type 'T' is not assignable to type 'ChannelOfType<T, TextChannel>["type"]'.
!!! error TS2322: Type 'string' is not assignable to type 'ChannelOfType<T, TextChannel>["type"]'.
!!! error TS2322: Type 'string' is not assignable to type 'ChannelOfType<T, TextChannel>["type"]'.
!!! error TS2322: Type 'string' is not assignable to type 'T & "text"'.
!!! error TS2322: Type 'string' is not assignable to type 'T'.
!!! error TS2322: Type '"text" | "email"' is not assignable to type 'ChannelOfType<T, TextChannel>["type"]'.
!!! error TS2322: Type '"text"' is not assignable to type 'ChannelOfType<T, TextChannel>["type"]'.
!!! error TS2322: Type '"text"' is not assignable to type 'T & "text"'.
!!! error TS2322: Type '"text"' is not assignable to type 'T'.
!!! error TS2322: '"text"' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '"text" | "email"'.
!!! error TS2322: Type 'T' is not assignable to type 'T & "text"'.
!!! error TS2322: Type 'string' is not assignable to type 'T & "text"'.
!!! error TS2322: Type 'string' is not assignable to type 'T & "text"'.
!!! error TS2322: Type 'string' is not assignable to type 'T'.
!!! error TS2322: Type '"text" | "email"' is not assignable to type 'T & "text"'.
!!! error TS2322: Type '"text"' is not assignable to type 'T & "text"'.
!!! error TS2322: Type '"text"' is not assignable to type 'T'.
!!! error TS2322: '"text"' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '"text" | "email"'.
!!! error TS2322: Type 'T' is not assignable to type '"text"'.
!!! error TS2322: Type '"text" | "email"' is not assignable to type '"text"'.
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/reference/conditionalTypes1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(159,5): error TS2
'ZeroOf<T>' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | number'.
Type '0 | (T extends string ? "" : false)' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to '0 | (T extends string ? "" : false)'.
Type 'number' is not assignable to type 'T'.
Type '0' is not assignable to type 'T'.
'0' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | number'.
Type 'string | number' is not assignable to type 'T'.
Type '"" | 0' is not assignable to type 'T'.
'"" | 0' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | number'.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm...

Type 'string' is not assignable to type 'T'.
Type '""' is not assignable to type 'T'.
'""' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | number'.
tests/cases/conformance/types/conditional/conditionalTypes1.ts(160,5): error TS2322: Type 'T' is not assignable to type 'ZeroOf<T>'.
Type 'string | number' is not assignable to type 'ZeroOf<T>'.
Expand Down Expand Up @@ -283,11 +283,11 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(288,43): error TS
!!! error TS2322: 'ZeroOf<T>' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | number'.
!!! error TS2322: Type '0 | (T extends string ? "" : false)' is not assignable to type 'T'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to '0 | (T extends string ? "" : false)'.
!!! error TS2322: Type 'number' is not assignable to type 'T'.
!!! error TS2322: Type '0' is not assignable to type 'T'.
!!! error TS2322: '0' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | number'.
!!! error TS2322: Type 'string | number' is not assignable to type 'T'.
!!! error TS2322: Type '"" | 0' is not assignable to type 'T'.
!!! error TS2322: '"" | 0' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | number'.
!!! error TS2322: Type 'string' is not assignable to type 'T'.
!!! error TS2322: Type '""' is not assignable to type 'T'.
!!! error TS2322: '""' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | number'.
y = x; // Error
~
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorImplementationWithDefaultValues2.ts(3,17): error TS2322: Type 'number' is not assignable to type 'string'.
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorImplementationWithDefaultValues2.ts(10,17): error TS2322: Type 'number' is not assignable to type 'T'.
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorImplementationWithDefaultValues2.ts(10,17): error TS2322: Type '1' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to '1'.
tests/cases/conformance/classes/constructorDeclarations/constructorParameters/constructorImplementationWithDefaultValues2.ts(10,27): error TS2322: Type 'T' is not assignable to type 'U'.
'U' could be instantiated with an arbitrary type which could be unrelated to 'T'.
Expand All @@ -21,7 +21,7 @@ tests/cases/conformance/classes/constructorDeclarations/constructorParameters/co
constructor(x: T, y: U);
constructor(x: T = 1, public y: U = x) { // error
~~~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'T'.
!!! error TS2322: Type '1' is not assignable to type 'T'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to '1'.
~~~~~~~~~~~~~~~
!!! error TS2322: Type 'T' is not assignable to type 'U'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ tests/cases/conformance/classes/members/inheritanceAndOverriding/derivedGenericC
tests/cases/conformance/classes/members/inheritanceAndOverriding/derivedGenericClassWithAny.ts(11,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/inheritanceAndOverriding/derivedGenericClassWithAny.ts(19,16): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/inheritanceAndOverriding/derivedGenericClassWithAny.ts(30,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/inheritanceAndOverriding/derivedGenericClassWithAny.ts(30,18): error TS2322: Type 'string' is not assignable to type 'T'.
tests/cases/conformance/classes/members/inheritanceAndOverriding/derivedGenericClassWithAny.ts(30,18): error TS2322: Type '""' is not assignable to type 'T'.
'""' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.
tests/cases/conformance/classes/members/inheritanceAndOverriding/derivedGenericClassWithAny.ts(32,9): error TS2322: Type 'string' is not assignable to type 'T'.
tests/cases/conformance/classes/members/inheritanceAndOverriding/derivedGenericClassWithAny.ts(32,9): error TS2322: Type '""' is not assignable to type 'T'.
'""' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.
tests/cases/conformance/classes/members/inheritanceAndOverriding/derivedGenericClassWithAny.ts(41,1): error TS2322: Type 'E<string>' is not assignable to type 'C<number>'.
Types of property 'x' are incompatible.
Expand Down Expand Up @@ -51,12 +51,12 @@ tests/cases/conformance/classes/members/inheritanceAndOverriding/derivedGenericC
~
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
~~~~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'T'.
!!! error TS2322: Type '""' is not assignable to type 'T'.
!!! error TS2322: '""' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.
foo(): T {
return ''; // error
~~~~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'T'.
!!! error TS2322: Type '""' is not assignable to type 'T'.
!!! error TS2322: '""' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFun
Type 'string' is not assignable to type '1'.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(30,23): error TS2345: Argument of type '(x: T) => string' is not assignable to parameter of type '(a: 1) => string'.
Types of parameters 'x' and 'a' are incompatible.
Type 'number' is not assignable to type 'T'.
Type '1' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to '1'.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(33,23): error TS2345: Argument of type '(x: T) => string' is not assignable to parameter of type '(a: 1) => string'.
Types of parameters 'x' and 'a' are incompatible.
Type 'number' is not assignable to type 'T'.
Type '1' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to '1'.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(34,24): error TS2345: Argument of type '(x: T) => string' is not assignable to parameter of type '(a: 1) => string'.
Types of parameters 'x' and 'a' are incompatible.
Type 'number' is not assignable to type 'T'.
Type '1' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to '1'.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(35,23): error TS2345: Argument of type '(a: number) => string' is not assignable to parameter of type '(a: number) => 1'.
Type 'string' is not assignable to type '1'.
Expand Down Expand Up @@ -53,21 +53,21 @@ tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFun
~~~~~~~~~~~~
!!! error TS2345: Argument of type '(x: T) => string' is not assignable to parameter of type '(a: 1) => string'.
!!! error TS2345: Types of parameters 'x' and 'a' are incompatible.
!!! error TS2345: Type 'number' is not assignable to type 'T'.
!!! error TS2345: Type '1' is not assignable to type 'T'.
!!! error TS2345: 'T' could be instantiated with an arbitrary type which could be unrelated to '1'.
var r10 = foo2(1, (x) => ''); // string

var r11 = foo3(1, (x: T) => '', ''); // error
~~~~~~~~~~~~
!!! error TS2345: Argument of type '(x: T) => string' is not assignable to parameter of type '(a: 1) => string'.
!!! error TS2345: Types of parameters 'x' and 'a' are incompatible.
!!! error TS2345: Type 'number' is not assignable to type 'T'.
!!! error TS2345: Type '1' is not assignable to type 'T'.
!!! error TS2345: 'T' could be instantiated with an arbitrary type which could be unrelated to '1'.
var r11b = foo3(1, (x: T) => '', 1); // error
~~~~~~~~~~~~
!!! error TS2345: Argument of type '(x: T) => string' is not assignable to parameter of type '(a: 1) => string'.
!!! error TS2345: Types of parameters 'x' and 'a' are incompatible.
!!! error TS2345: Type 'number' is not assignable to type 'T'.
!!! error TS2345: Type '1' is not assignable to type 'T'.
!!! error TS2345: 'T' could be instantiated with an arbitrary type which could be unrelated to '1'.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm...

var r12 = foo3(1, function (a) { return '' }, 1); // error
~~~~~~~~
Expand Down
Loading