forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consider index signatures as optional properties in contextual type d…
…iscrimination (microsoft#54596)
- Loading branch information
Showing
5 changed files
with
194 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
tests/baselines/reference/discriminatedUnionWithIndexSignature.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}; |
59 changes: 59 additions & 0 deletions
59
tests/baselines/reference/discriminatedUnionWithIndexSignature.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
}, | ||
}; |
55 changes: 55 additions & 0 deletions
55
tests/baselines/reference/discriminatedUnionWithIndexSignature.types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
tests/cases/compiler/discriminatedUnionWithIndexSignature.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}; |