Skip to content
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

Allow getLiteralTypeFromProperty to extract name types from unions #60994

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18330,6 +18330,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
type = prop.escapedName === InternalSymbolName.Default ? getStringLiteralType("default") :
name && getLiteralTypeFromPropertyName(name) || (!isKnownSymbol(prop) ? getStringLiteralType(symbolName(prop)) : undefined);
}
// Property name can only have a union type if it refers to multiple structurally identical declarations. (See addMemberForKeyTypeWorker)
// In this case, it's safe to take any of the constituents to compute the literal type.
if (type && type.flags & TypeFlags.Union) {
const unionType = type as UnionType;
if (unionType.types.length > 0) {
type = unionType.types[0];
}
}
if (type && type.flags & include) {
return type;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//// [tests/cases/compiler/mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts] ////

//// [mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts]
// https://github.com/microsoft/TypeScript/issues/41700

enum EnumA {
A = 'A',
B = 'B',
}

// A second enum with at least one key also in EnumA
enum EnumB {
B = 'B',
C = 'C',
}

type Mapped = {
[k in EnumA|EnumB]: string;
};

// Should work
const partial: Partial<Mapped> = {
[EnumA.B]: 'value',
};


//// [mappedTypeOverMappedTypeWithOverlappingEnumKeys.js]
// https://github.com/microsoft/TypeScript/issues/41700
var _a;
var EnumA;
(function (EnumA) {
EnumA["A"] = "A";
EnumA["B"] = "B";
})(EnumA || (EnumA = {}));
// A second enum with at least one key also in EnumA
var EnumB;
(function (EnumB) {
EnumB["B"] = "B";
EnumB["C"] = "C";
})(EnumB || (EnumB = {}));
// Should work
var partial = (_a = {},
_a[EnumA.B] = 'value',
_a);
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//// [tests/cases/compiler/mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts] ////

=== mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts ===
// https://github.com/microsoft/TypeScript/issues/41700

enum EnumA {
>EnumA : Symbol(EnumA, Decl(mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts, 0, 0))

A = 'A',
>A : Symbol(EnumA.A, Decl(mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts, 2, 12))

B = 'B',
>B : Symbol(EnumA.B, Decl(mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts, 3, 12))
}

// A second enum with at least one key also in EnumA
enum EnumB {
>EnumB : Symbol(EnumB, Decl(mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts, 5, 1))

B = 'B',
>B : Symbol(EnumB.B, Decl(mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts, 8, 12))

C = 'C',
>C : Symbol(EnumB.C, Decl(mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts, 9, 12))
}

type Mapped = {
>Mapped : Symbol(Mapped, Decl(mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts, 11, 1))

[k in EnumA|EnumB]: string;
>k : Symbol(k, Decl(mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts, 14, 5))
>EnumA : Symbol(EnumA, Decl(mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts, 0, 0))
>EnumB : Symbol(EnumB, Decl(mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts, 5, 1))

};

// Should work
const partial: Partial<Mapped> = {
>partial : Symbol(partial, Decl(mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts, 18, 5))
>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --))
>Mapped : Symbol(Mapped, Decl(mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts, 11, 1))

[EnumA.B]: 'value',
>[EnumA.B] : Symbol([EnumA.B], Decl(mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts, 18, 34))
>EnumA.B : Symbol(EnumA.B, Decl(mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts, 3, 12))
>EnumA : Symbol(EnumA, Decl(mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts, 0, 0))
>B : Symbol(EnumA.B, Decl(mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts, 3, 12))

};

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//// [tests/cases/compiler/mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts] ////

=== mappedTypeOverMappedTypeWithOverlappingEnumKeys.ts ===
// https://github.com/microsoft/TypeScript/issues/41700

enum EnumA {
>EnumA : EnumA
> : ^^^^^

A = 'A',
>A : EnumA.A
> : ^^^^^^^
>'A' : "A"
> : ^^^

B = 'B',
>B : EnumA.B
> : ^^^^^^^
>'B' : "B"
> : ^^^
}

// A second enum with at least one key also in EnumA
enum EnumB {
>EnumB : EnumB
> : ^^^^^

B = 'B',
>B : EnumB.B
> : ^^^^^^^
>'B' : "B"
> : ^^^

C = 'C',
>C : EnumB.C
> : ^^^^^^^
>'C' : "C"
> : ^^^
}

type Mapped = {
>Mapped : Mapped
> : ^^^^^^

[k in EnumA|EnumB]: string;
};

// Should work
const partial: Partial<Mapped> = {
>partial : Partial<Mapped>
> : ^^^^^^^^^^^^^^^
>{ [EnumA.B]: 'value',} : { B: string; }
> : ^^^^^^^^^^^^^^

[EnumA.B]: 'value',
>[EnumA.B] : string
> : ^^^^^^
>EnumA.B : EnumA.B
> : ^^^^^^^
>EnumA : typeof EnumA
> : ^^^^^^^^^^^^
>B : EnumA.B
> : ^^^^^^^
>'value' : "value"
> : ^^^^^^^

};

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// https://github.com/microsoft/TypeScript/issues/41700

enum EnumA {
A = 'A',
B = 'B',
}

// A second enum with at least one key also in EnumA
enum EnumB {
B = 'B',
C = 'C',
}

type Mapped = {
[k in EnumA|EnumB]: string;
};

// Should work
const partial: Partial<Mapped> = {
[EnumA.B]: 'value',
};
Loading