Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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: 10 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22701,7 +22701,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 = getTypeOfPropertyOrOptionalIndexSignature(types[i], propertyName);
if (targetType && related(getDiscriminatingType(), targetType)) {
matched = true;
}
Expand All @@ -22719,6 +22719,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
const filtered = contains(include, Ternary.False) ? getUnionType(types.filter((_, i) => include[i])) : target;
return filtered.flags & TypeFlags.Never ? target : filtered;

function getTypeOfPropertyOrOptionalIndexSignature(type: Type, name: __String): Type | undefined {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this function might already exist, but I couldn't find it.

Copy link
Copy Markdown
Member

@DanielRosenwasser DanielRosenwasser Jun 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's getTypeOfPropertyOrIndexSignature (used by narrowTypeByDiscriminant!). Consider replacing that function and making it take a parameter on whether to add optionality for index signatures.

let propType = getTypeOfPropertyOfType(type, name);
if (propType) {
return propType;
}
propType = getApplicableIndexInfoForName(type, name)?.type;
return propType && addOptionality(propType, /*isProperty*/ true, /*isOptional*/ true);
}
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/baselines/reference/discriminatedUnionWithIndexSignature.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//// [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,57 @@
=== tests/cases/compiler/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,53 @@
=== tests/cases/compiler/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,
},
};