From 4777d6eed461ae29d5e554df08b71ea709552e37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Wed, 1 May 2024 15:26:37 +0200 Subject: [PATCH] Skip single call signature comparisons in strict variance --- src/compiler/checker.ts | 4 +- .../reference/genericRestTypes.errors.txt | 10 +++-- .../strictFunctionTypesErrors.errors.txt | 42 +++++++------------ 3 files changed, 23 insertions(+), 33 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 1595e7a6f5331..05784c554515a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -20777,8 +20777,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { // similar to return values, callback parameters are output positions. This means that a Promise, // where T is used only in callback parameter positions, will be co-variant (as opposed to bi-variant) // with respect to T. - const sourceSig = checkMode & SignatureCheckMode.Callback || isInstantiatedGenericParameter(source, i) ? undefined : getSingleCallSignature(getNonNullableType(sourceType)); - const targetSig = checkMode & SignatureCheckMode.Callback || isInstantiatedGenericParameter(target, i) ? undefined : getSingleCallSignature(getNonNullableType(targetType)); + const sourceSig = checkMode & SignatureCheckMode.Callback || strictVariance || isInstantiatedGenericParameter(source, i) ? undefined : getSingleCallSignature(getNonNullableType(sourceType)); + const targetSig = checkMode & SignatureCheckMode.Callback || strictVariance || isInstantiatedGenericParameter(target, i) ? undefined : getSingleCallSignature(getNonNullableType(targetType)); const callbacks = sourceSig && targetSig && !getTypePredicateOfSignature(sourceSig) && !getTypePredicateOfSignature(targetSig) && getTypeFacts(sourceType, TypeFacts.IsUndefinedOrNull) === getTypeFacts(targetType, TypeFacts.IsUndefinedOrNull); let related = callbacks ? diff --git a/tests/baselines/reference/genericRestTypes.errors.txt b/tests/baselines/reference/genericRestTypes.errors.txt index 684a55b492c41..1df17e93d2f8b 100644 --- a/tests/baselines/reference/genericRestTypes.errors.txt +++ b/tests/baselines/reference/genericRestTypes.errors.txt @@ -1,7 +1,8 @@ genericRestTypes.ts(21,11): error TS2322: Type '(cb: (x: string, ...rest: T) => void) => void' is not assignable to type '(cb: (...args: never) => void) => void'. Types of parameters 'cb' and 'cb' are incompatible. - Types of parameters 'args' and 'x' are incompatible. - Type '[x: string, ...rest: T]' is not assignable to type 'never'. + Type '(...args: never) => void' is not assignable to type '(x: string, ...rest: T) => void'. + Types of parameters 'args' and 'x' are incompatible. + Type '[x: string, ...rest: T]' is not assignable to type 'never'. ==== genericRestTypes.ts (1 errors) ==== @@ -29,8 +30,9 @@ genericRestTypes.ts(21,11): error TS2322: Type '(cb: (x: string, ...rest: T) => ~~~ !!! error TS2322: Type '(cb: (x: string, ...rest: T) => void) => void' is not assignable to type '(cb: (...args: never) => void) => void'. !!! error TS2322: Types of parameters 'cb' and 'cb' are incompatible. -!!! error TS2322: Types of parameters 'args' and 'x' are incompatible. -!!! error TS2322: Type '[x: string, ...rest: T]' is not assignable to type 'never'. +!!! error TS2322: Type '(...args: never) => void' is not assignable to type '(x: string, ...rest: T) => void'. +!!! error TS2322: Types of parameters 'args' and 'x' are incompatible. +!!! error TS2322: Type '[x: string, ...rest: T]' is not assignable to type 'never'. } function assignmentWithComplexRest3() { diff --git a/tests/baselines/reference/strictFunctionTypesErrors.errors.txt b/tests/baselines/reference/strictFunctionTypesErrors.errors.txt index 0b5ad51922316..688750e059503 100644 --- a/tests/baselines/reference/strictFunctionTypesErrors.errors.txt +++ b/tests/baselines/reference/strictFunctionTypesErrors.errors.txt @@ -69,23 +69,18 @@ strictFunctionTypesErrors.ts(126,1): error TS2322: Type 'Crate' is not assi strictFunctionTypesErrors.ts(127,1): error TS2322: Type 'Crate' is not assignable to type 'Crate'. Types of property 'item' are incompatible. Type 'Animal' is not assignable to type 'Dog'. -strictFunctionTypesErrors.ts(133,1): error TS2328: Types of parameters 'f' and 'f' are incompatible. - Type 'Animal' is not assignable to type 'Dog'. -strictFunctionTypesErrors.ts(134,1): error TS2322: Type '(f: (x: Animal) => Animal) => void' is not assignable to type '(f: (x: Dog) => Dog) => void'. +strictFunctionTypesErrors.ts(133,1): error TS2322: Type '(f: (x: Dog) => Dog) => void' is not assignable to type '(f: (x: Animal) => Animal) => void'. Types of parameters 'f' and 'f' are incompatible. - Types of parameters 'x' and 'x' are incompatible. - Type 'Animal' is not assignable to type 'Dog'. -strictFunctionTypesErrors.ts(147,5): error TS2322: Type '(cb: (x: Animal) => Animal) => void' is not assignable to type '(cb: (x: Dog) => Animal) => void'. - Types of parameters 'cb' and 'cb' are incompatible. - Types of parameters 'x' and 'x' are incompatible. - Type 'Animal' is not assignable to type 'Dog'. -strictFunctionTypesErrors.ts(155,5): error TS2322: Type '(cb: (x: Animal) => Animal) => void' is not assignable to type '(cb: (x: Dog) => Animal) => void'. - Types of parameters 'cb' and 'cb' are incompatible. - Types of parameters 'x' and 'x' are incompatible. + Type '(x: Animal) => Animal' is not assignable to type '(x: Dog) => Dog'. Type 'Animal' is not assignable to type 'Dog'. +strictFunctionTypesErrors.ts(134,1): error TS2322: Type '(f: (x: Animal) => Animal) => void' is not assignable to type '(f: (x: Dog) => Dog) => void'. + Types of parameters 'f' and 'f' are incompatible. + Type '(x: Dog) => Dog' is not assignable to type '(x: Animal) => Animal'. + Types of parameters 'x' and 'x' are incompatible. + Type 'Animal' is not assignable to type 'Dog'. -==== strictFunctionTypesErrors.ts (35 errors) ==== +==== strictFunctionTypesErrors.ts (33 errors) ==== export {} @@ -323,14 +318,17 @@ strictFunctionTypesErrors.ts(155,5): error TS2322: Type '(cb: (x: Animal) => Ani declare let fc2: (f: (x: Dog) => Dog) => void; fc1 = fc2; // Error ~~~ -!!! error TS2328: Types of parameters 'f' and 'f' are incompatible. -!!! error TS2328: Type 'Animal' is not assignable to type 'Dog'. +!!! error TS2322: Type '(f: (x: Dog) => Dog) => void' is not assignable to type '(f: (x: Animal) => Animal) => void'. +!!! error TS2322: Types of parameters 'f' and 'f' are incompatible. +!!! error TS2322: Type '(x: Animal) => Animal' is not assignable to type '(x: Dog) => Dog'. +!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. fc2 = fc1; // Error ~~~ !!! error TS2322: Type '(f: (x: Animal) => Animal) => void' is not assignable to type '(f: (x: Dog) => Dog) => void'. !!! error TS2322: Types of parameters 'f' and 'f' are incompatible. -!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. -!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. +!!! error TS2322: Type '(x: Dog) => Dog' is not assignable to type '(x: Animal) => Animal'. +!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. +!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. // Verify that callback parameters aren't loosely checked when types // originate in method declarations @@ -344,11 +342,6 @@ strictFunctionTypesErrors.ts(155,5): error TS2322: Type '(cb: (x: Animal) => Ani declare let f2: (cb: typeof Foo.f2) => void; f1 = f2; f2 = f1; // Error - ~~ -!!! error TS2322: Type '(cb: (x: Animal) => Animal) => void' is not assignable to type '(cb: (x: Dog) => Animal) => void'. -!!! error TS2322: Types of parameters 'cb' and 'cb' are incompatible. -!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. -!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. } namespace n2 { @@ -357,9 +350,4 @@ strictFunctionTypesErrors.ts(155,5): error TS2322: Type '(cb: (x: Animal) => Ani declare let f2: (cb: BivariantHack) => void; f1 = f2; f2 = f1; // Error - ~~ -!!! error TS2322: Type '(cb: (x: Animal) => Animal) => void' is not assignable to type '(cb: (x: Dog) => Animal) => void'. -!!! error TS2322: Types of parameters 'cb' and 'cb' are incompatible. -!!! error TS2322: Types of parameters 'x' and 'x' are incompatible. -!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'. } \ No newline at end of file