Skip to content

Commit 03951f2

Browse files
authored
Consider index signatures as optional properties in contextual type discrimination (#54596)
1 parent a2968a1 commit 03951f2

5 files changed

+194
-4
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10165,8 +10165,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1016510165
return prop ? getTypeOfSymbol(prop) : undefined;
1016610166
}
1016710167

10168-
function getTypeOfPropertyOrIndexSignature(type: Type, name: __String): Type {
10169-
return getTypeOfPropertyOfType(type, name) || getApplicableIndexInfoForName(type, name)?.type || unknownType;
10168+
function getTypeOfPropertyOrIndexSignature(type: Type, name: __String, addOptionalityToIndex: boolean): Type {
10169+
let propType;
10170+
return getTypeOfPropertyOfType(type, name) ||
10171+
(propType = getApplicableIndexInfoForName(type, name)?.type) && addOptionality(propType, /*isProperty*/ true, addOptionalityToIndex) ||
10172+
unknownType;
1017010173
}
1017110174

1017210175
function isTypeAny(type: Type | undefined) {
@@ -22700,7 +22703,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2270022703
let matched = false;
2270122704
for (let i = 0; i < types.length; i++) {
2270222705
if (include[i]) {
22703-
const targetType = getTypeOfPropertyOfType(types[i], propertyName);
22706+
const targetType = getTypeOfPropertyOrIndexSignature(types[i], propertyName, /*addOptionalityToIndex*/ true);
2270422707
if (targetType && related(getDiscriminatingType(), targetType)) {
2270522708
matched = true;
2270622709
}
@@ -27013,7 +27016,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2701327016
propType = removeNullable && optionalChain ? getOptionalType(propType) : propType;
2701427017
const narrowedPropType = narrowType(propType);
2701527018
return filterType(type, t => {
27016-
const discriminantType = getTypeOfPropertyOrIndexSignature(t, propName);
27019+
const discriminantType = getTypeOfPropertyOrIndexSignature(t, propName, /*addOptionalityToIndex*/ false);
2701727020
return !(discriminantType.flags & TypeFlags.Never) && !(narrowedPropType.flags & TypeFlags.Never) && areTypesComparable(narrowedPropType, discriminantType);
2701827021
});
2701927022
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//// [tests/cases/compiler/discriminatedUnionWithIndexSignature.ts] ////
2+
3+
//// [discriminatedUnionWithIndexSignature.ts]
4+
export interface UnionAltA {
5+
type?: 'text';
6+
}
7+
8+
export interface UnionAltB {
9+
type?: 'image' | 'video' | 'document';
10+
}
11+
12+
export type ValueUnion = UnionAltA | UnionAltB;
13+
14+
export type MapOrSingleton =
15+
| {
16+
[key: string]: ValueUnion;
17+
}
18+
| ValueUnion;
19+
20+
const withoutAsConst: MapOrSingleton = {
21+
1: {
22+
type: 'text' /*as const*/,
23+
},
24+
};
25+
26+
const withAsConst: MapOrSingleton = {
27+
1: {
28+
type: 'text' as const,
29+
},
30+
};
31+
32+
//// [discriminatedUnionWithIndexSignature.js]
33+
"use strict";
34+
Object.defineProperty(exports, "__esModule", { value: true });
35+
var withoutAsConst = {
36+
1: {
37+
type: 'text' /*as const*/,
38+
},
39+
};
40+
var withAsConst = {
41+
1: {
42+
type: 'text',
43+
},
44+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//// [tests/cases/compiler/discriminatedUnionWithIndexSignature.ts] ////
2+
3+
=== discriminatedUnionWithIndexSignature.ts ===
4+
export interface UnionAltA {
5+
>UnionAltA : Symbol(UnionAltA, Decl(discriminatedUnionWithIndexSignature.ts, 0, 0))
6+
7+
type?: 'text';
8+
>type : Symbol(UnionAltA.type, Decl(discriminatedUnionWithIndexSignature.ts, 0, 28))
9+
}
10+
11+
export interface UnionAltB {
12+
>UnionAltB : Symbol(UnionAltB, Decl(discriminatedUnionWithIndexSignature.ts, 2, 1))
13+
14+
type?: 'image' | 'video' | 'document';
15+
>type : Symbol(UnionAltB.type, Decl(discriminatedUnionWithIndexSignature.ts, 4, 28))
16+
}
17+
18+
export type ValueUnion = UnionAltA | UnionAltB;
19+
>ValueUnion : Symbol(ValueUnion, Decl(discriminatedUnionWithIndexSignature.ts, 6, 1))
20+
>UnionAltA : Symbol(UnionAltA, Decl(discriminatedUnionWithIndexSignature.ts, 0, 0))
21+
>UnionAltB : Symbol(UnionAltB, Decl(discriminatedUnionWithIndexSignature.ts, 2, 1))
22+
23+
export type MapOrSingleton =
24+
>MapOrSingleton : Symbol(MapOrSingleton, Decl(discriminatedUnionWithIndexSignature.ts, 8, 47))
25+
26+
| {
27+
[key: string]: ValueUnion;
28+
>key : Symbol(key, Decl(discriminatedUnionWithIndexSignature.ts, 12, 9))
29+
>ValueUnion : Symbol(ValueUnion, Decl(discriminatedUnionWithIndexSignature.ts, 6, 1))
30+
}
31+
| ValueUnion;
32+
>ValueUnion : Symbol(ValueUnion, Decl(discriminatedUnionWithIndexSignature.ts, 6, 1))
33+
34+
const withoutAsConst: MapOrSingleton = {
35+
>withoutAsConst : Symbol(withoutAsConst, Decl(discriminatedUnionWithIndexSignature.ts, 16, 5))
36+
>MapOrSingleton : Symbol(MapOrSingleton, Decl(discriminatedUnionWithIndexSignature.ts, 8, 47))
37+
38+
1: {
39+
>1 : Symbol(1, Decl(discriminatedUnionWithIndexSignature.ts, 16, 40))
40+
41+
type: 'text' /*as const*/,
42+
>type : Symbol(type, Decl(discriminatedUnionWithIndexSignature.ts, 17, 8))
43+
44+
},
45+
};
46+
47+
const withAsConst: MapOrSingleton = {
48+
>withAsConst : Symbol(withAsConst, Decl(discriminatedUnionWithIndexSignature.ts, 22, 5))
49+
>MapOrSingleton : Symbol(MapOrSingleton, Decl(discriminatedUnionWithIndexSignature.ts, 8, 47))
50+
51+
1: {
52+
>1 : Symbol(1, Decl(discriminatedUnionWithIndexSignature.ts, 22, 37))
53+
54+
type: 'text' as const,
55+
>type : Symbol(type, Decl(discriminatedUnionWithIndexSignature.ts, 23, 8))
56+
>const : Symbol(const)
57+
58+
},
59+
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//// [tests/cases/compiler/discriminatedUnionWithIndexSignature.ts] ////
2+
3+
=== discriminatedUnionWithIndexSignature.ts ===
4+
export interface UnionAltA {
5+
type?: 'text';
6+
>type : "text" | undefined
7+
}
8+
9+
export interface UnionAltB {
10+
type?: 'image' | 'video' | 'document';
11+
>type : "image" | "video" | "document" | undefined
12+
}
13+
14+
export type ValueUnion = UnionAltA | UnionAltB;
15+
>ValueUnion : UnionAltA | UnionAltB
16+
17+
export type MapOrSingleton =
18+
>MapOrSingleton : ValueUnion | { [key: string]: ValueUnion; }
19+
20+
| {
21+
[key: string]: ValueUnion;
22+
>key : string
23+
}
24+
| ValueUnion;
25+
26+
const withoutAsConst: MapOrSingleton = {
27+
>withoutAsConst : MapOrSingleton
28+
>{ 1: { type: 'text' /*as const*/, },} : { 1: { type: "text"; }; }
29+
30+
1: {
31+
>1 : { type: "text"; }
32+
>{ type: 'text' /*as const*/, } : { type: "text"; }
33+
34+
type: 'text' /*as const*/,
35+
>type : "text"
36+
>'text' : "text"
37+
38+
},
39+
};
40+
41+
const withAsConst: MapOrSingleton = {
42+
>withAsConst : MapOrSingleton
43+
>{ 1: { type: 'text' as const, },} : { 1: { type: "text"; }; }
44+
45+
1: {
46+
>1 : { type: "text"; }
47+
>{ type: 'text' as const, } : { type: "text"; }
48+
49+
type: 'text' as const,
50+
>type : "text"
51+
>'text' as const : "text"
52+
>'text' : "text"
53+
54+
},
55+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// @strict: true
2+
3+
export interface UnionAltA {
4+
type?: 'text';
5+
}
6+
7+
export interface UnionAltB {
8+
type?: 'image' | 'video' | 'document';
9+
}
10+
11+
export type ValueUnion = UnionAltA | UnionAltB;
12+
13+
export type MapOrSingleton =
14+
| {
15+
[key: string]: ValueUnion;
16+
}
17+
| ValueUnion;
18+
19+
const withoutAsConst: MapOrSingleton = {
20+
1: {
21+
type: 'text' /*as const*/,
22+
},
23+
};
24+
25+
const withAsConst: MapOrSingleton = {
26+
1: {
27+
type: 'text' as const,
28+
},
29+
};

0 commit comments

Comments
 (0)