Skip to content

Commit 47b60ec

Browse files
authored
Make substitution types even if the substitution base isnt a type variable (microsoft#37348)
* Make substitution types even if the substitution base isnt a type variable * Broaden usage of getConditionalFlowTypeOfType to _all_ type lookups * Align comments
1 parent db44231 commit 47b60ec

8 files changed

+175
-24
lines changed

src/compiler/checker.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4278,7 +4278,7 @@ namespace ts {
42784278
return createConditionalTypeNode(checkTypeNode, extendsTypeNode, trueTypeNode, falseTypeNode);
42794279
}
42804280
if (type.flags & TypeFlags.Substitution) {
4281-
return typeToTypeNodeHelper((<SubstitutionType>type).typeVariable, context);
4281+
return typeToTypeNodeHelper((<SubstitutionType>type).baseType, context);
42824282
}
42834283

42844284
return Debug.fail("Should be unreachable.");
@@ -11327,9 +11327,7 @@ namespace ts {
1132711327
// Get type from reference to named type that cannot be generic (enum or type parameter)
1132811328
const res = tryGetDeclaredTypeOfSymbol(symbol);
1132911329
if (res) {
11330-
return checkNoTypeArguments(node, symbol) ?
11331-
res.flags & TypeFlags.TypeParameter ? getConstrainedTypeVariable(<TypeParameter>res, node) : getRegularTypeOfLiteralType(res) :
11332-
errorType;
11330+
return checkNoTypeArguments(node, symbol) ? getRegularTypeOfLiteralType(res) : errorType;
1133311331
}
1133411332
if (symbol.flags & SymbolFlags.Value && isJSDocTypeReference(node)) {
1133511333
const jsdocType = getTypeFromJSDocValueReference(node, symbol);
@@ -11377,17 +11375,17 @@ namespace ts {
1137711375
return links.resolvedJSDocType;
1137811376
}
1137911377

11380-
function getSubstitutionType(typeVariable: TypeVariable, substitute: Type) {
11381-
if (substitute.flags & TypeFlags.AnyOrUnknown || substitute === typeVariable) {
11382-
return typeVariable;
11378+
function getSubstitutionType(baseType: Type, substitute: Type) {
11379+
if (substitute.flags & TypeFlags.AnyOrUnknown || substitute === baseType) {
11380+
return baseType;
1138311381
}
11384-
const id = `${getTypeId(typeVariable)}>${getTypeId(substitute)}`;
11382+
const id = `${getTypeId(baseType)}>${getTypeId(substitute)}`;
1138511383
const cached = substitutionTypes.get(id);
1138611384
if (cached) {
1138711385
return cached;
1138811386
}
1138911387
const result = <SubstitutionType>createType(TypeFlags.Substitution);
11390-
result.typeVariable = typeVariable;
11388+
result.baseType = baseType;
1139111389
result.substitute = substitute;
1139211390
substitutionTypes.set(id, result);
1139311391
return result;
@@ -11397,25 +11395,25 @@ namespace ts {
1139711395
return node.kind === SyntaxKind.TupleType && (<TupleTypeNode>node).elementTypes.length === 1;
1139811396
}
1139911397

11400-
function getImpliedConstraint(typeVariable: TypeVariable, checkNode: TypeNode, extendsNode: TypeNode): Type | undefined {
11401-
return isUnaryTupleTypeNode(checkNode) && isUnaryTupleTypeNode(extendsNode) ? getImpliedConstraint(typeVariable, (<TupleTypeNode>checkNode).elementTypes[0], (<TupleTypeNode>extendsNode).elementTypes[0]) :
11402-
getActualTypeVariable(getTypeFromTypeNode(checkNode)) === typeVariable ? getTypeFromTypeNode(extendsNode) :
11398+
function getImpliedConstraint(type: Type, checkNode: TypeNode, extendsNode: TypeNode): Type | undefined {
11399+
return isUnaryTupleTypeNode(checkNode) && isUnaryTupleTypeNode(extendsNode) ? getImpliedConstraint(type, (<TupleTypeNode>checkNode).elementTypes[0], (<TupleTypeNode>extendsNode).elementTypes[0]) :
11400+
getActualTypeVariable(getTypeFromTypeNode(checkNode)) === type ? getTypeFromTypeNode(extendsNode) :
1140311401
undefined;
1140411402
}
1140511403

11406-
function getConstrainedTypeVariable(typeVariable: TypeVariable, node: Node) {
11404+
function getConditionalFlowTypeOfType(type: Type, node: Node) {
1140711405
let constraints: Type[] | undefined;
1140811406
while (node && !isStatement(node) && node.kind !== SyntaxKind.JSDocComment) {
1140911407
const parent = node.parent;
1141011408
if (parent.kind === SyntaxKind.ConditionalType && node === (<ConditionalTypeNode>parent).trueType) {
11411-
const constraint = getImpliedConstraint(typeVariable, (<ConditionalTypeNode>parent).checkType, (<ConditionalTypeNode>parent).extendsType);
11409+
const constraint = getImpliedConstraint(type, (<ConditionalTypeNode>parent).checkType, (<ConditionalTypeNode>parent).extendsType);
1141211410
if (constraint) {
1141311411
constraints = append(constraints, constraint);
1141411412
}
1141511413
}
1141611414
node = parent;
1141711415
}
11418-
return constraints ? getSubstitutionType(typeVariable, getIntersectionType(append(constraints, typeVariable))) : typeVariable;
11416+
return constraints ? getSubstitutionType(type, getIntersectionType(append(constraints, type))) : type;
1141911417
}
1142011418

1142111419
function isJSDocTypeReference(node: Node): node is TypeReferenceNode {
@@ -12858,7 +12856,7 @@ namespace ts {
1285812856
links.resolvedType = resolved.flags & TypeFlags.IndexedAccess &&
1285912857
(<IndexedAccessType>resolved).objectType === objectType &&
1286012858
(<IndexedAccessType>resolved).indexType === indexType ?
12861-
getConstrainedTypeVariable(<IndexedAccessType>resolved, node) : resolved;
12859+
getConditionalFlowTypeOfType(resolved, node) : resolved;
1286212860
}
1286312861
return links.resolvedType;
1286412862
}
@@ -12880,7 +12878,7 @@ namespace ts {
1288012878

1288112879
function getActualTypeVariable(type: Type): Type {
1288212880
if (type.flags & TypeFlags.Substitution) {
12883-
return (<SubstitutionType>type).typeVariable;
12881+
return (<SubstitutionType>type).baseType;
1288412882
}
1288512883
if (type.flags & TypeFlags.IndexedAccess && (
1288612884
(<IndexedAccessType>type).objectType.flags & TypeFlags.Substitution ||
@@ -13428,6 +13426,10 @@ namespace ts {
1342813426
}
1342913427

1343013428
function getTypeFromTypeNode(node: TypeNode): Type {
13429+
return getConditionalFlowTypeOfType(getTypeFromTypeNodeWorker(node), node);
13430+
}
13431+
13432+
function getTypeFromTypeNodeWorker(node: TypeNode): Type {
1343113433
switch (node.kind) {
1343213434
case SyntaxKind.AnyKeyword:
1343313435
case SyntaxKind.JSDocAllType:
@@ -13775,7 +13777,7 @@ namespace ts {
1377513777
return !!tp.isThisType;
1377613778
case SyntaxKind.Identifier:
1377713779
return !tp.isThisType && isPartOfTypeNode(node) && maybeTypeParameterReference(node) &&
13778-
getTypeFromTypeNode(<TypeNode>node) === tp;
13780+
getTypeFromTypeNodeWorker(<TypeNode>node) === tp; // use worker because we're looking for === equality
1377913781
case SyntaxKind.TypeQuery:
1378013782
return true;
1378113783
}
@@ -13976,7 +13978,7 @@ namespace ts {
1397613978
return getConditionalTypeInstantiation(<ConditionalType>type, combineTypeMappers((<ConditionalType>type).mapper, mapper));
1397713979
}
1397813980
if (flags & TypeFlags.Substitution) {
13979-
const maybeVariable = instantiateType((<SubstitutionType>type).typeVariable, mapper);
13981+
const maybeVariable = instantiateType((<SubstitutionType>type).baseType, mapper);
1398013982
if (maybeVariable.flags & TypeFlags.TypeVariable) {
1398113983
return getSubstitutionType(maybeVariable as TypeVariable, instantiateType((<SubstitutionType>type).substitute, mapper));
1398213984
}
@@ -14985,7 +14987,7 @@ namespace ts {
1498514987
const t = isFreshLiteralType(type) ? (<FreshableType>type).regularType :
1498614988
getObjectFlags(type) & ObjectFlags.Reference && (<TypeReference>type).node ? createTypeReference((<TypeReference>type).target, getTypeArguments(<TypeReference>type)) :
1498714989
type.flags & TypeFlags.UnionOrIntersection ? getReducedType(type) :
14988-
type.flags & TypeFlags.Substitution ? writing ? (<SubstitutionType>type).typeVariable : (<SubstitutionType>type).substitute :
14990+
type.flags & TypeFlags.Substitution ? writing ? (<SubstitutionType>type).baseType : (<SubstitutionType>type).substitute :
1498914991
type.flags & TypeFlags.Simplifiable ? getSimplifiedType(type, writing) :
1499014992
type;
1499114993
if (t === type) break;

src/compiler/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4808,8 +4808,8 @@ namespace ts {
48084808
// Thus, if Foo has a 'string' constraint on its type parameter, T will satisfy it. Substitution
48094809
// types disappear upon instantiation (just like type parameters).
48104810
export interface SubstitutionType extends InstantiableType {
4811-
typeVariable: TypeVariable; // Target type variable
4812-
substitute: Type; // Type to substitute for type parameter
4811+
baseType: Type; // Target type
4812+
substitute: Type; // Type to substitute for type parameter
48134813
}
48144814

48154815
/* @internal */

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2523,7 +2523,7 @@ declare namespace ts {
25232523
resolvedFalseType: Type;
25242524
}
25252525
export interface SubstitutionType extends InstantiableType {
2526-
typeVariable: TypeVariable;
2526+
baseType: Type;
25272527
substitute: Type;
25282528
}
25292529
export enum SignatureKind {

tests/baselines/reference/api/typescript.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2523,7 +2523,7 @@ declare namespace ts {
25232523
resolvedFalseType: Type;
25242524
}
25252525
export interface SubstitutionType extends InstantiableType {
2526-
typeVariable: TypeVariable;
2526+
baseType: Type;
25272527
substitute: Type;
25282528
}
25292529
export enum SignatureKind {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//// [inlineConditionalHasSimilarAssignability.ts]
2+
type MyExtract<T, U> = T extends U ? T : never
3+
4+
function foo<T>(a: T) {
5+
const b: Extract<any[], T> = 0 as any;
6+
a = b; // ok
7+
8+
const c: (any[] extends T ? any[] : never) = 0 as any;
9+
a = c;
10+
11+
const d: MyExtract<any[], T> = 0 as any;
12+
a = d; // ok
13+
14+
type CustomType = any[] extends T ? any[] : never;
15+
const e: CustomType = 0 as any;
16+
a = e;
17+
}
18+
19+
//// [inlineConditionalHasSimilarAssignability.js]
20+
function foo(a) {
21+
var b = 0;
22+
a = b; // ok
23+
var c = 0;
24+
a = c;
25+
var d = 0;
26+
a = d; // ok
27+
var e = 0;
28+
a = e;
29+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
=== tests/cases/compiler/inlineConditionalHasSimilarAssignability.ts ===
2+
type MyExtract<T, U> = T extends U ? T : never
3+
>MyExtract : Symbol(MyExtract, Decl(inlineConditionalHasSimilarAssignability.ts, 0, 0))
4+
>T : Symbol(T, Decl(inlineConditionalHasSimilarAssignability.ts, 0, 15))
5+
>U : Symbol(U, Decl(inlineConditionalHasSimilarAssignability.ts, 0, 17))
6+
>T : Symbol(T, Decl(inlineConditionalHasSimilarAssignability.ts, 0, 15))
7+
>U : Symbol(U, Decl(inlineConditionalHasSimilarAssignability.ts, 0, 17))
8+
>T : Symbol(T, Decl(inlineConditionalHasSimilarAssignability.ts, 0, 15))
9+
10+
function foo<T>(a: T) {
11+
>foo : Symbol(foo, Decl(inlineConditionalHasSimilarAssignability.ts, 0, 46))
12+
>T : Symbol(T, Decl(inlineConditionalHasSimilarAssignability.ts, 2, 13))
13+
>a : Symbol(a, Decl(inlineConditionalHasSimilarAssignability.ts, 2, 16))
14+
>T : Symbol(T, Decl(inlineConditionalHasSimilarAssignability.ts, 2, 13))
15+
16+
const b: Extract<any[], T> = 0 as any;
17+
>b : Symbol(b, Decl(inlineConditionalHasSimilarAssignability.ts, 3, 7))
18+
>Extract : Symbol(Extract, Decl(lib.es5.d.ts, --, --))
19+
>T : Symbol(T, Decl(inlineConditionalHasSimilarAssignability.ts, 2, 13))
20+
21+
a = b; // ok
22+
>a : Symbol(a, Decl(inlineConditionalHasSimilarAssignability.ts, 2, 16))
23+
>b : Symbol(b, Decl(inlineConditionalHasSimilarAssignability.ts, 3, 7))
24+
25+
const c: (any[] extends T ? any[] : never) = 0 as any;
26+
>c : Symbol(c, Decl(inlineConditionalHasSimilarAssignability.ts, 6, 7))
27+
>T : Symbol(T, Decl(inlineConditionalHasSimilarAssignability.ts, 2, 13))
28+
29+
a = c;
30+
>a : Symbol(a, Decl(inlineConditionalHasSimilarAssignability.ts, 2, 16))
31+
>c : Symbol(c, Decl(inlineConditionalHasSimilarAssignability.ts, 6, 7))
32+
33+
const d: MyExtract<any[], T> = 0 as any;
34+
>d : Symbol(d, Decl(inlineConditionalHasSimilarAssignability.ts, 9, 7))
35+
>MyExtract : Symbol(MyExtract, Decl(inlineConditionalHasSimilarAssignability.ts, 0, 0))
36+
>T : Symbol(T, Decl(inlineConditionalHasSimilarAssignability.ts, 2, 13))
37+
38+
a = d; // ok
39+
>a : Symbol(a, Decl(inlineConditionalHasSimilarAssignability.ts, 2, 16))
40+
>d : Symbol(d, Decl(inlineConditionalHasSimilarAssignability.ts, 9, 7))
41+
42+
type CustomType = any[] extends T ? any[] : never;
43+
>CustomType : Symbol(CustomType, Decl(inlineConditionalHasSimilarAssignability.ts, 10, 8))
44+
>T : Symbol(T, Decl(inlineConditionalHasSimilarAssignability.ts, 2, 13))
45+
46+
const e: CustomType = 0 as any;
47+
>e : Symbol(e, Decl(inlineConditionalHasSimilarAssignability.ts, 13, 7))
48+
>CustomType : Symbol(CustomType, Decl(inlineConditionalHasSimilarAssignability.ts, 10, 8))
49+
50+
a = e;
51+
>a : Symbol(a, Decl(inlineConditionalHasSimilarAssignability.ts, 2, 16))
52+
>e : Symbol(e, Decl(inlineConditionalHasSimilarAssignability.ts, 13, 7))
53+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
=== tests/cases/compiler/inlineConditionalHasSimilarAssignability.ts ===
2+
type MyExtract<T, U> = T extends U ? T : never
3+
>MyExtract : MyExtract<T, U>
4+
5+
function foo<T>(a: T) {
6+
>foo : <T>(a: T) => void
7+
>a : T
8+
9+
const b: Extract<any[], T> = 0 as any;
10+
>b : Extract<any[], T>
11+
>0 as any : any
12+
>0 : 0
13+
14+
a = b; // ok
15+
>a = b : Extract<any[], T>
16+
>a : T
17+
>b : Extract<any[], T>
18+
19+
const c: (any[] extends T ? any[] : never) = 0 as any;
20+
>c : any[] extends T ? any[] : never
21+
>0 as any : any
22+
>0 : 0
23+
24+
a = c;
25+
>a = c : any[] extends T ? any[] : never
26+
>a : T
27+
>c : any[] extends T ? any[] : never
28+
29+
const d: MyExtract<any[], T> = 0 as any;
30+
>d : MyExtract<any[], T>
31+
>0 as any : any
32+
>0 : 0
33+
34+
a = d; // ok
35+
>a = d : MyExtract<any[], T>
36+
>a : T
37+
>d : MyExtract<any[], T>
38+
39+
type CustomType = any[] extends T ? any[] : never;
40+
>CustomType : any[] extends T ? any[] : never
41+
42+
const e: CustomType = 0 as any;
43+
>e : any[] extends T ? any[] : never
44+
>0 as any : any
45+
>0 : 0
46+
47+
a = e;
48+
>a = e : any[] extends T ? any[] : never
49+
>a : T
50+
>e : any[] extends T ? any[] : never
51+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
type MyExtract<T, U> = T extends U ? T : never
2+
3+
function foo<T>(a: T) {
4+
const b: Extract<any[], T> = 0 as any;
5+
a = b; // ok
6+
7+
const c: (any[] extends T ? any[] : never) = 0 as any;
8+
a = c;
9+
10+
const d: MyExtract<any[], T> = 0 as any;
11+
a = d; // ok
12+
13+
type CustomType = any[] extends T ? any[] : never;
14+
const e: CustomType = 0 as any;
15+
a = e;
16+
}

0 commit comments

Comments
 (0)