mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Consider index signatures as optional properties in contextual type discrimination (#54596)
This commit is contained in:
@@ -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,
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user