mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-15 03:23:08 -06:00
Merge pull request #31662 from microsoft/fixTypeAliasInference
Fix contravariant type alias inference
This commit is contained in:
commit
82ea88569e
@ -15171,11 +15171,7 @@ namespace ts {
|
||||
if (source.aliasSymbol && source.aliasTypeArguments && source.aliasSymbol === target.aliasSymbol) {
|
||||
// Source and target are types originating in the same generic type alias declaration.
|
||||
// Simply infer from source type arguments to target type arguments.
|
||||
const sourceTypes = source.aliasTypeArguments;
|
||||
const targetTypes = target.aliasTypeArguments!;
|
||||
for (let i = 0; i < sourceTypes.length; i++) {
|
||||
inferFromTypes(sourceTypes[i], targetTypes[i]);
|
||||
}
|
||||
inferFromTypeArguments(source.aliasTypeArguments, target.aliasTypeArguments!, getAliasVariances(source.aliasSymbol));
|
||||
return;
|
||||
}
|
||||
if (source.flags & TypeFlags.Union && target.flags & TypeFlags.Union && !(source.flags & TypeFlags.EnumLiteral && target.flags & TypeFlags.EnumLiteral) ||
|
||||
@ -15281,18 +15277,7 @@ namespace ts {
|
||||
}
|
||||
if (getObjectFlags(source) & ObjectFlags.Reference && getObjectFlags(target) & ObjectFlags.Reference && (<TypeReference>source).target === (<TypeReference>target).target) {
|
||||
// If source and target are references to the same generic type, infer from type arguments
|
||||
const sourceTypes = (<TypeReference>source).typeArguments || emptyArray;
|
||||
const targetTypes = (<TypeReference>target).typeArguments || emptyArray;
|
||||
const count = sourceTypes.length < targetTypes.length ? sourceTypes.length : targetTypes.length;
|
||||
const variances = getVariances((<TypeReference>source).target);
|
||||
for (let i = 0; i < count; i++) {
|
||||
if (i < variances.length && (variances[i] & VarianceFlags.VarianceMask) === VarianceFlags.Contravariant) {
|
||||
inferFromContravariantTypes(sourceTypes[i], targetTypes[i]);
|
||||
}
|
||||
else {
|
||||
inferFromTypes(sourceTypes[i], targetTypes[i]);
|
||||
}
|
||||
}
|
||||
inferFromTypeArguments((<TypeReference>source).typeArguments || emptyArray, (<TypeReference>target).typeArguments || emptyArray, getVariances((<TypeReference>source).target));
|
||||
}
|
||||
else if (source.flags & TypeFlags.Index && target.flags & TypeFlags.Index) {
|
||||
contravariant = !contravariant;
|
||||
@ -15412,6 +15397,18 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
function inferFromTypeArguments(sourceTypes: readonly Type[], targetTypes: readonly Type[], variances: readonly VarianceFlags[]) {
|
||||
const count = sourceTypes.length < targetTypes.length ? sourceTypes.length : targetTypes.length;
|
||||
for (let i = 0; i < count; i++) {
|
||||
if (i < variances.length && (variances[i] & VarianceFlags.VarianceMask) === VarianceFlags.Contravariant) {
|
||||
inferFromContravariantTypes(sourceTypes[i], targetTypes[i]);
|
||||
}
|
||||
else {
|
||||
inferFromTypes(sourceTypes[i], targetTypes[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function inferFromContravariantTypes(source: Type, target: Type) {
|
||||
if (strictFunctionTypes || priority & InferencePriority.AlwaysStrict) {
|
||||
contravariant = !contravariant;
|
||||
|
||||
25
tests/baselines/reference/contravariantTypeAliasInference.js
Normal file
25
tests/baselines/reference/contravariantTypeAliasInference.js
Normal file
@ -0,0 +1,25 @@
|
||||
//// [contravariantTypeAliasInference.ts]
|
||||
type Func1<T> = (x: T) => void;
|
||||
type Func2<T> = ((x: T) => void) | undefined;
|
||||
|
||||
declare let f1: Func1<string>;
|
||||
declare let f2: Func1<"a">;
|
||||
|
||||
declare function foo<T>(f1: Func1<T>, f2: Func1<T>): void;
|
||||
|
||||
foo(f1, f2);
|
||||
|
||||
declare let g1: Func2<string>;
|
||||
declare let g2: Func2<"a">;
|
||||
|
||||
declare function bar<T>(g1: Func2<T>, g2: Func2<T>): void;
|
||||
|
||||
bar(f1, f2);
|
||||
bar(g1, g2);
|
||||
|
||||
|
||||
//// [contravariantTypeAliasInference.js]
|
||||
"use strict";
|
||||
foo(f1, f2);
|
||||
bar(f1, f2);
|
||||
bar(g1, g2);
|
||||
@ -0,0 +1,64 @@
|
||||
=== tests/cases/compiler/contravariantTypeAliasInference.ts ===
|
||||
type Func1<T> = (x: T) => void;
|
||||
>Func1 : Symbol(Func1, Decl(contravariantTypeAliasInference.ts, 0, 0))
|
||||
>T : Symbol(T, Decl(contravariantTypeAliasInference.ts, 0, 11))
|
||||
>x : Symbol(x, Decl(contravariantTypeAliasInference.ts, 0, 17))
|
||||
>T : Symbol(T, Decl(contravariantTypeAliasInference.ts, 0, 11))
|
||||
|
||||
type Func2<T> = ((x: T) => void) | undefined;
|
||||
>Func2 : Symbol(Func2, Decl(contravariantTypeAliasInference.ts, 0, 31))
|
||||
>T : Symbol(T, Decl(contravariantTypeAliasInference.ts, 1, 11))
|
||||
>x : Symbol(x, Decl(contravariantTypeAliasInference.ts, 1, 18))
|
||||
>T : Symbol(T, Decl(contravariantTypeAliasInference.ts, 1, 11))
|
||||
|
||||
declare let f1: Func1<string>;
|
||||
>f1 : Symbol(f1, Decl(contravariantTypeAliasInference.ts, 3, 11))
|
||||
>Func1 : Symbol(Func1, Decl(contravariantTypeAliasInference.ts, 0, 0))
|
||||
|
||||
declare let f2: Func1<"a">;
|
||||
>f2 : Symbol(f2, Decl(contravariantTypeAliasInference.ts, 4, 11))
|
||||
>Func1 : Symbol(Func1, Decl(contravariantTypeAliasInference.ts, 0, 0))
|
||||
|
||||
declare function foo<T>(f1: Func1<T>, f2: Func1<T>): void;
|
||||
>foo : Symbol(foo, Decl(contravariantTypeAliasInference.ts, 4, 27))
|
||||
>T : Symbol(T, Decl(contravariantTypeAliasInference.ts, 6, 21))
|
||||
>f1 : Symbol(f1, Decl(contravariantTypeAliasInference.ts, 6, 24))
|
||||
>Func1 : Symbol(Func1, Decl(contravariantTypeAliasInference.ts, 0, 0))
|
||||
>T : Symbol(T, Decl(contravariantTypeAliasInference.ts, 6, 21))
|
||||
>f2 : Symbol(f2, Decl(contravariantTypeAliasInference.ts, 6, 37))
|
||||
>Func1 : Symbol(Func1, Decl(contravariantTypeAliasInference.ts, 0, 0))
|
||||
>T : Symbol(T, Decl(contravariantTypeAliasInference.ts, 6, 21))
|
||||
|
||||
foo(f1, f2);
|
||||
>foo : Symbol(foo, Decl(contravariantTypeAliasInference.ts, 4, 27))
|
||||
>f1 : Symbol(f1, Decl(contravariantTypeAliasInference.ts, 3, 11))
|
||||
>f2 : Symbol(f2, Decl(contravariantTypeAliasInference.ts, 4, 11))
|
||||
|
||||
declare let g1: Func2<string>;
|
||||
>g1 : Symbol(g1, Decl(contravariantTypeAliasInference.ts, 10, 11))
|
||||
>Func2 : Symbol(Func2, Decl(contravariantTypeAliasInference.ts, 0, 31))
|
||||
|
||||
declare let g2: Func2<"a">;
|
||||
>g2 : Symbol(g2, Decl(contravariantTypeAliasInference.ts, 11, 11))
|
||||
>Func2 : Symbol(Func2, Decl(contravariantTypeAliasInference.ts, 0, 31))
|
||||
|
||||
declare function bar<T>(g1: Func2<T>, g2: Func2<T>): void;
|
||||
>bar : Symbol(bar, Decl(contravariantTypeAliasInference.ts, 11, 27))
|
||||
>T : Symbol(T, Decl(contravariantTypeAliasInference.ts, 13, 21))
|
||||
>g1 : Symbol(g1, Decl(contravariantTypeAliasInference.ts, 13, 24))
|
||||
>Func2 : Symbol(Func2, Decl(contravariantTypeAliasInference.ts, 0, 31))
|
||||
>T : Symbol(T, Decl(contravariantTypeAliasInference.ts, 13, 21))
|
||||
>g2 : Symbol(g2, Decl(contravariantTypeAliasInference.ts, 13, 37))
|
||||
>Func2 : Symbol(Func2, Decl(contravariantTypeAliasInference.ts, 0, 31))
|
||||
>T : Symbol(T, Decl(contravariantTypeAliasInference.ts, 13, 21))
|
||||
|
||||
bar(f1, f2);
|
||||
>bar : Symbol(bar, Decl(contravariantTypeAliasInference.ts, 11, 27))
|
||||
>f1 : Symbol(f1, Decl(contravariantTypeAliasInference.ts, 3, 11))
|
||||
>f2 : Symbol(f2, Decl(contravariantTypeAliasInference.ts, 4, 11))
|
||||
|
||||
bar(g1, g2);
|
||||
>bar : Symbol(bar, Decl(contravariantTypeAliasInference.ts, 11, 27))
|
||||
>g1 : Symbol(g1, Decl(contravariantTypeAliasInference.ts, 10, 11))
|
||||
>g2 : Symbol(g2, Decl(contravariantTypeAliasInference.ts, 11, 11))
|
||||
|
||||
@ -0,0 +1,49 @@
|
||||
=== tests/cases/compiler/contravariantTypeAliasInference.ts ===
|
||||
type Func1<T> = (x: T) => void;
|
||||
>Func1 : Func1<T>
|
||||
>x : T
|
||||
|
||||
type Func2<T> = ((x: T) => void) | undefined;
|
||||
>Func2 : Func2<T>
|
||||
>x : T
|
||||
|
||||
declare let f1: Func1<string>;
|
||||
>f1 : Func1<string>
|
||||
|
||||
declare let f2: Func1<"a">;
|
||||
>f2 : Func1<"a">
|
||||
|
||||
declare function foo<T>(f1: Func1<T>, f2: Func1<T>): void;
|
||||
>foo : <T>(f1: Func1<T>, f2: Func1<T>) => void
|
||||
>f1 : Func1<T>
|
||||
>f2 : Func1<T>
|
||||
|
||||
foo(f1, f2);
|
||||
>foo(f1, f2) : void
|
||||
>foo : <T>(f1: Func1<T>, f2: Func1<T>) => void
|
||||
>f1 : Func1<string>
|
||||
>f2 : Func1<"a">
|
||||
|
||||
declare let g1: Func2<string>;
|
||||
>g1 : Func2<string>
|
||||
|
||||
declare let g2: Func2<"a">;
|
||||
>g2 : Func2<"a">
|
||||
|
||||
declare function bar<T>(g1: Func2<T>, g2: Func2<T>): void;
|
||||
>bar : <T>(g1: Func2<T>, g2: Func2<T>) => void
|
||||
>g1 : Func2<T>
|
||||
>g2 : Func2<T>
|
||||
|
||||
bar(f1, f2);
|
||||
>bar(f1, f2) : void
|
||||
>bar : <T>(g1: Func2<T>, g2: Func2<T>) => void
|
||||
>f1 : Func1<string>
|
||||
>f2 : Func1<"a">
|
||||
|
||||
bar(g1, g2);
|
||||
>bar(g1, g2) : void
|
||||
>bar : <T>(g1: Func2<T>, g2: Func2<T>) => void
|
||||
>g1 : Func2<string>
|
||||
>g2 : Func2<"a">
|
||||
|
||||
19
tests/cases/compiler/contravariantTypeAliasInference.ts
Normal file
19
tests/cases/compiler/contravariantTypeAliasInference.ts
Normal file
@ -0,0 +1,19 @@
|
||||
// @strict: true
|
||||
|
||||
type Func1<T> = (x: T) => void;
|
||||
type Func2<T> = ((x: T) => void) | undefined;
|
||||
|
||||
declare let f1: Func1<string>;
|
||||
declare let f2: Func1<"a">;
|
||||
|
||||
declare function foo<T>(f1: Func1<T>, f2: Func1<T>): void;
|
||||
|
||||
foo(f1, f2);
|
||||
|
||||
declare let g1: Func2<string>;
|
||||
declare let g2: Func2<"a">;
|
||||
|
||||
declare function bar<T>(g1: Func2<T>, g2: Func2<T>): void;
|
||||
|
||||
bar(f1, f2);
|
||||
bar(g1, g2);
|
||||
Loading…
x
Reference in New Issue
Block a user