Consider index signatures as optional properties in contextual type discrimination (#54596)

This commit is contained in:
Gabriela Araujo Britto
2023-06-13 09:57:51 -07:00
committed by GitHub
parent a2968a1620
commit 03951f2ddf
5 changed files with 194 additions and 4 deletions
+7 -4
View File
@@ -10165,8 +10165,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return prop ? getTypeOfSymbol(prop) : undefined;
}
function getTypeOfPropertyOrIndexSignature(type: Type, name: __String): Type {
return getTypeOfPropertyOfType(type, name) || getApplicableIndexInfoForName(type, name)?.type || unknownType;
function getTypeOfPropertyOrIndexSignature(type: Type, name: __String, addOptionalityToIndex: boolean): Type {
let propType;
return getTypeOfPropertyOfType(type, name) ||
(propType = getApplicableIndexInfoForName(type, name)?.type) && addOptionality(propType, /*isProperty*/ true, addOptionalityToIndex) ||
unknownType;
}
function isTypeAny(type: Type | undefined) {
@@ -22700,7 +22703,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
let matched = false;
for (let i = 0; i < types.length; i++) {
if (include[i]) {
const targetType = getTypeOfPropertyOfType(types[i], propertyName);
const targetType = getTypeOfPropertyOrIndexSignature(types[i], propertyName, /*addOptionalityToIndex*/ true);
if (targetType && related(getDiscriminatingType(), targetType)) {
matched = true;
}
@@ -27013,7 +27016,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
propType = removeNullable && optionalChain ? getOptionalType(propType) : propType;
const narrowedPropType = narrowType(propType);
return filterType(type, t => {
const discriminantType = getTypeOfPropertyOrIndexSignature(t, propName);
const discriminantType = getTypeOfPropertyOrIndexSignature(t, propName, /*addOptionalityToIndex*/ false);
return !(discriminantType.flags & TypeFlags.Never) && !(narrowedPropType.flags & TypeFlags.Never) && areTypesComparable(narrowedPropType, discriminantType);
});
}
@@ -0,0 +1,44 @@
//// [tests/cases/compiler/discriminatedUnionWithIndexSignature.ts] ////
//// [discriminatedUnionWithIndexSignature.ts]
export interface UnionAltA {
type?: 'text';
}
export interface UnionAltB {
type?: 'image' | 'video' | 'document';
}
export type ValueUnion = UnionAltA | UnionAltB;
export type MapOrSingleton =
| {
[key: string]: ValueUnion;
}
| ValueUnion;
const withoutAsConst: MapOrSingleton = {
1: {
type: 'text' /*as const*/,
},
};
const withAsConst: MapOrSingleton = {
1: {
type: 'text' as const,
},
};
//// [discriminatedUnionWithIndexSignature.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var withoutAsConst = {
1: {
type: 'text' /*as const*/,
},
};
var withAsConst = {
1: {
type: 'text',
},
};
@@ -0,0 +1,59 @@
//// [tests/cases/compiler/discriminatedUnionWithIndexSignature.ts] ////
=== discriminatedUnionWithIndexSignature.ts ===
export interface UnionAltA {
>UnionAltA : Symbol(UnionAltA, Decl(discriminatedUnionWithIndexSignature.ts, 0, 0))
type?: 'text';
>type : Symbol(UnionAltA.type, Decl(discriminatedUnionWithIndexSignature.ts, 0, 28))
}
export interface UnionAltB {
>UnionAltB : Symbol(UnionAltB, Decl(discriminatedUnionWithIndexSignature.ts, 2, 1))
type?: 'image' | 'video' | 'document';
>type : Symbol(UnionAltB.type, Decl(discriminatedUnionWithIndexSignature.ts, 4, 28))
}
export type ValueUnion = UnionAltA | UnionAltB;
>ValueUnion : Symbol(ValueUnion, Decl(discriminatedUnionWithIndexSignature.ts, 6, 1))
>UnionAltA : Symbol(UnionAltA, Decl(discriminatedUnionWithIndexSignature.ts, 0, 0))
>UnionAltB : Symbol(UnionAltB, Decl(discriminatedUnionWithIndexSignature.ts, 2, 1))
export type MapOrSingleton =
>MapOrSingleton : Symbol(MapOrSingleton, Decl(discriminatedUnionWithIndexSignature.ts, 8, 47))
| {
[key: string]: ValueUnion;
>key : Symbol(key, Decl(discriminatedUnionWithIndexSignature.ts, 12, 9))
>ValueUnion : Symbol(ValueUnion, Decl(discriminatedUnionWithIndexSignature.ts, 6, 1))
}
| ValueUnion;
>ValueUnion : Symbol(ValueUnion, Decl(discriminatedUnionWithIndexSignature.ts, 6, 1))
const withoutAsConst: MapOrSingleton = {
>withoutAsConst : Symbol(withoutAsConst, Decl(discriminatedUnionWithIndexSignature.ts, 16, 5))
>MapOrSingleton : Symbol(MapOrSingleton, Decl(discriminatedUnionWithIndexSignature.ts, 8, 47))
1: {
>1 : Symbol(1, Decl(discriminatedUnionWithIndexSignature.ts, 16, 40))
type: 'text' /*as const*/,
>type : Symbol(type, Decl(discriminatedUnionWithIndexSignature.ts, 17, 8))
},
};
const withAsConst: MapOrSingleton = {
>withAsConst : Symbol(withAsConst, Decl(discriminatedUnionWithIndexSignature.ts, 22, 5))
>MapOrSingleton : Symbol(MapOrSingleton, Decl(discriminatedUnionWithIndexSignature.ts, 8, 47))
1: {
>1 : Symbol(1, Decl(discriminatedUnionWithIndexSignature.ts, 22, 37))
type: 'text' as const,
>type : Symbol(type, Decl(discriminatedUnionWithIndexSignature.ts, 23, 8))
>const : Symbol(const)
},
};
@@ -0,0 +1,55 @@
//// [tests/cases/compiler/discriminatedUnionWithIndexSignature.ts] ////
=== discriminatedUnionWithIndexSignature.ts ===
export interface UnionAltA {
type?: 'text';
>type : "text" | undefined
}
export interface UnionAltB {
type?: 'image' | 'video' | 'document';
>type : "image" | "video" | "document" | undefined
}
export type ValueUnion = UnionAltA | UnionAltB;
>ValueUnion : UnionAltA | UnionAltB
export type MapOrSingleton =
>MapOrSingleton : ValueUnion | { [key: string]: ValueUnion; }
| {
[key: string]: ValueUnion;
>key : string
}
| ValueUnion;
const withoutAsConst: MapOrSingleton = {
>withoutAsConst : MapOrSingleton
>{ 1: { type: 'text' /*as const*/, },} : { 1: { type: "text"; }; }
1: {
>1 : { type: "text"; }
>{ type: 'text' /*as const*/, } : { type: "text"; }
type: 'text' /*as const*/,
>type : "text"
>'text' : "text"
},
};
const withAsConst: MapOrSingleton = {
>withAsConst : MapOrSingleton
>{ 1: { type: 'text' as const, },} : { 1: { type: "text"; }; }
1: {
>1 : { type: "text"; }
>{ type: 'text' as const, } : { type: "text"; }
type: 'text' as const,
>type : "text"
>'text' as const : "text"
>'text' : "text"
},
};
@@ -0,0 +1,29 @@
// @strict: true
export interface UnionAltA {
type?: 'text';
}
export interface UnionAltB {
type?: 'image' | 'video' | 'document';
}
export type ValueUnion = UnionAltA | UnionAltB;
export type MapOrSingleton =
| {
[key: string]: ValueUnion;
}
| ValueUnion;
const withoutAsConst: MapOrSingleton = {
1: {
type: 'text' /*as const*/,
},
};
const withAsConst: MapOrSingleton = {
1: {
type: 'text' as const,
},
};