microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.01k stars 12.48k forks source link

Cannot use nesting computed property name in an interface #33009

Open zbinlin opened 5 years ago

zbinlin commented 5 years ago

TypeScript Version: 3.5.1

Code

const enum Types {
    FOO = 0,
};

const Names = {
    [Types.FOO]: 'foo',
} as const;

interface Baz {
    [Names[Types.FOO]]: string;
}

Expected behavior: No error

Actual behavior: Error: A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.

Playground Link: https://www.typescriptlang.org/play/index.html#code/MYewdgzgLgBApmArgWxgFQJ4Ac4RgbwFgAoGMmAMQHkqYBeGABgBoSBfAbhJNElgDkAhslz0CJcjADamHBAB01KgF0AXDADkAMxAgNrYmxiC8vaF2IkAlmChwATlsHA4MAEKCAXuNLkpQkQgZbFxFGmU1GGh7GwBzCzYgA

afisheronetrust commented 3 years ago

I'm getting the same thing but in an even more basic example on the playground.

type Literal = 'test';

interface SomeObject {
  [Literal]: boolean;
}

let sym = Symbol('test');

interface AnotherObject {
  [sym]: boolean;
}

A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.

'Literal' only refers to a type, but is being used as a value here.

A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.

Playground