Skip to content

Commit

Permalink
Skip single call signature comparisons in strict variance
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed May 1, 2024
1 parent 749bd83 commit 4777d6e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20777,8 +20777,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// similar to return values, callback parameters are output positions. This means that a Promise<T>,
// 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 ?
Expand Down
10 changes: 6 additions & 4 deletions tests/baselines/reference/genericRestTypes.errors.txt
Original file line number Diff line number Diff line change
@@ -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) ====
Expand Down Expand Up @@ -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<T extends any[]>() {
Expand Down
42 changes: 15 additions & 27 deletions tests/baselines/reference/strictFunctionTypesErrors.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,18 @@ strictFunctionTypesErrors.ts(126,1): error TS2322: Type 'Crate<Dog>' is not assi
strictFunctionTypesErrors.ts(127,1): error TS2322: Type 'Crate<Animal>' is not assignable to type 'Crate<Dog>'.
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 {}


Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -357,9 +350,4 @@ strictFunctionTypesErrors.ts(155,5): error TS2322: Type '(cb: (x: Animal) => Ani
declare let f2: (cb: BivariantHack<Dog, Animal>) => 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'.
}

0 comments on commit 4777d6e

Please sign in to comment.