Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10161,8 +10161,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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
});
}
Expand Down
44 changes: 44 additions & 0 deletions tests/baselines/reference/discriminatedUnionWithIndexSignature.js
Original file line number Diff line number Diff line change
@@ -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',
},
};
Original file line number Diff line number Diff line change
@@ -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)

},
};
Original file line number Diff line number Diff line change
@@ -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"

},
};
29 changes: 29 additions & 0 deletions tests/cases/compiler/discriminatedUnionWithIndexSignature.ts
Original file line number Diff line number Diff line change
@@ -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,
},
};