Skip to content
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
2 changes: 1 addition & 1 deletion tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27802,7 +27802,7 @@ func (c *Checker) checkComputedPropertyName(node *ast.Node) *Type {
links.resolvedType = c.errorType
return links.resolvedType
}
links.resolvedType = c.checkExpression(node.Expression())
links.resolvedType = c.getRegularTypeOfLiteralType(c.checkExpression(node.Expression()))
Comment thread
ahejlsberg marked this conversation as resolved.
Resolved
// This will allow types number, string, symbol or any. It will also allow enums, the unknown
// type, and any union of these types (like string | number).
if links.resolvedType.flags&TypeFlagsNullable != 0 ||
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//// [tests/cases/compiler/computedMemberNamesNonWidening.ts] ////

=== computedMemberNamesNonWidening.ts ===
// https://github.com/microsoft/TypeScript/issues/64437

const K1 = 'a', K2 = 'b';
>K1 : Symbol(K1, Decl(computedMemberNamesNonWidening.ts, 2, 5))
>K2 : Symbol(K2, Decl(computedMemberNamesNonWidening.ts, 2, 15))

// Computed keys
const Computed = {[K1]: 'x', [K2]: 'y'} as const;
>Computed : Symbol(Computed, Decl(computedMemberNamesNonWidening.ts, 5, 5))
>[K1] : Symbol([K1], Decl(computedMemberNamesNonWidening.ts, 5, 18))
>K1 : Symbol(K1, Decl(computedMemberNamesNonWidening.ts, 2, 5))
>[K2] : Symbol([K2], Decl(computedMemberNamesNonWidening.ts, 5, 28))
>K2 : Symbol(K2, Decl(computedMemberNamesNonWidening.ts, 2, 15))
>const : Symbol(const)

declare function computedKey(): keyof typeof Computed;
>computedKey : Symbol(computedKey, Decl(computedMemberNamesNonWidening.ts, 5, 49))
>Computed : Symbol(Computed, Decl(computedMemberNamesNonWidening.ts, 5, 5))

const wrapComputed = () => ({key: computedKey()});
>wrapComputed : Symbol(wrapComputed, Decl(computedMemberNamesNonWidening.ts, 7, 5))
>key : Symbol(key, Decl(computedMemberNamesNonWidening.ts, 7, 29))
>computedKey : Symbol(computedKey, Decl(computedMemberNamesNonWidening.ts, 5, 49))

export const fromComputed: {key: 'a' | 'b'} = wrapComputed();
>fromComputed : Symbol(fromComputed, Decl(computedMemberNamesNonWidening.ts, 8, 12))
>key : Symbol(key, Decl(computedMemberNamesNonWidening.ts, 8, 28))
>wrapComputed : Symbol(wrapComputed, Decl(computedMemberNamesNonWidening.ts, 7, 5))

// Literal keys
const Literal = {a: 'x', b: 'y'} as const;
>Literal : Symbol(Literal, Decl(computedMemberNamesNonWidening.ts, 11, 5))
>a : Symbol(a, Decl(computedMemberNamesNonWidening.ts, 11, 17))
>b : Symbol(b, Decl(computedMemberNamesNonWidening.ts, 11, 24))
>const : Symbol(const)

declare function literalKey(): keyof typeof Literal;
>literalKey : Symbol(literalKey, Decl(computedMemberNamesNonWidening.ts, 11, 42))
>Literal : Symbol(Literal, Decl(computedMemberNamesNonWidening.ts, 11, 5))

const wrapLiteral = () => ({key: literalKey()});
>wrapLiteral : Symbol(wrapLiteral, Decl(computedMemberNamesNonWidening.ts, 13, 5))
>key : Symbol(key, Decl(computedMemberNamesNonWidening.ts, 13, 28))
>literalKey : Symbol(literalKey, Decl(computedMemberNamesNonWidening.ts, 11, 42))

export const fromLiteral: {key: 'a' | 'b'} = wrapLiteral();
>fromLiteral : Symbol(fromLiteral, Decl(computedMemberNamesNonWidening.ts, 14, 12))
>key : Symbol(key, Decl(computedMemberNamesNonWidening.ts, 14, 27))
>wrapLiteral : Symbol(wrapLiteral, Decl(computedMemberNamesNonWidening.ts, 13, 5))

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

=== computedMemberNamesNonWidening.ts ===
// https://github.com/microsoft/TypeScript/issues/64437

const K1 = 'a', K2 = 'b';
>K1 : "a"
>'a' : "a"
>K2 : "b"
>'b' : "b"

// Computed keys
const Computed = {[K1]: 'x', [K2]: 'y'} as const;
>Computed : { readonly a: 'x'; readonly b: 'y'; }
>{[K1]: 'x', [K2]: 'y'} as const : { readonly a: 'x'; readonly b: 'y'; }
>{[K1]: 'x', [K2]: 'y'} : { readonly a: 'x'; readonly b: 'y'; }
>[K1] : "x"
>K1 : "a"
>'x' : "x"
>[K2] : "y"
>K2 : "b"
>'y' : "y"

declare function computedKey(): keyof typeof Computed;
>computedKey : () => keyof typeof Computed
>Computed : { readonly a: 'x'; readonly b: 'y'; }

const wrapComputed = () => ({key: computedKey()});
>wrapComputed : () => { key: "a" | "b"; }
>() => ({key: computedKey()}) : () => { key: "a" | "b"; }
>({key: computedKey()}) : { key: "a" | "b"; }
>{key: computedKey()} : { key: "a" | "b"; }
>key : "a" | "b"
>computedKey() : "a" | "b"
>computedKey : () => keyof typeof Computed

export const fromComputed: {key: 'a' | 'b'} = wrapComputed();
>fromComputed : { key: 'a' | 'b'; }
>key : "a" | "b"
>wrapComputed() : { key: "a" | "b"; }
>wrapComputed : () => { key: "a" | "b"; }

// Literal keys
const Literal = {a: 'x', b: 'y'} as const;
>Literal : { readonly a: 'x'; readonly b: 'y'; }
>{a: 'x', b: 'y'} as const : { readonly a: 'x'; readonly b: 'y'; }
>{a: 'x', b: 'y'} : { readonly a: 'x'; readonly b: 'y'; }
>a : "x"
>'x' : "x"
>b : "y"
>'y' : "y"

declare function literalKey(): keyof typeof Literal;
>literalKey : () => keyof typeof Literal
>Literal : { readonly a: 'x'; readonly b: 'y'; }

const wrapLiteral = () => ({key: literalKey()});
>wrapLiteral : () => { key: "a" | "b"; }
>() => ({key: literalKey()}) : () => { key: "a" | "b"; }
>({key: literalKey()}) : { key: "a" | "b"; }
>{key: literalKey()} : { key: "a" | "b"; }
>key : "a" | "b"
>literalKey() : "a" | "b"
>literalKey : () => keyof typeof Literal

export const fromLiteral: {key: 'a' | 'b'} = wrapLiteral();
>fromLiteral : { key: 'a' | 'b'; }
>key : "a" | "b"
>wrapLiteral() : { key: "a" | "b"; }
>wrapLiteral : () => { key: "a" | "b"; }

Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export namespace Namespace {
} as const;

export type Key = keyof typeof record;
>Key : NamespaceEnum.A
>Key : NamespaceEnum
>record : { readonly [NamespaceEnum.A]: NamespaceEnum; }

type NamespaceAssignability<T extends NamespaceEnum> = T;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @noEmit: true

// https://github.com/microsoft/TypeScript/issues/64437

const K1 = 'a', K2 = 'b';

// Computed keys
const Computed = {[K1]: 'x', [K2]: 'y'} as const;
declare function computedKey(): keyof typeof Computed;
const wrapComputed = () => ({key: computedKey()});
export const fromComputed: {key: 'a' | 'b'} = wrapComputed();

// Literal keys
const Literal = {a: 'x', b: 'y'} as const;
declare function literalKey(): keyof typeof Literal;
const wrapLiteral = () => ({key: literalKey()});
export const fromLiteral: {key: 'a' | 'b'} = wrapLiteral();
Loading