Skip to content

Request: Remove the some type restrictions on Computed Properties syntax. #17942

Closed
@zheeeng

Description

@zheeeng

TypeScript Version: 2.4.0

Code

In ES6 Computed Properties syntax, we are allowed to use any type as the property of an object. e.g.:

const s = Symbol()

// `[ts] A computed property name must be of type 'string', 'number', 'symbol', or 'any'.`
const awesomeObj = {
    [() => {}]: 'function',
    [null]: 'null',
    [undefined]: 'undefined',
    [s]: 'symbol',
}

console.log(Object.keys(awesomeObj)) // ["() => {}", "null", "undefined"]

Above are valid usages, but TS tips me: [ts] A computed property name must be of type 'string', 'number', 'symbol', or 'any'.

Another non-sense restriction is we can't set symbols or any other types exclude string and number as object's indexes while we defining object interface.

--del--

// `An index signature parameter type must be 'string' or 'number'.`
interface IAwesomeObj {
    [key: any]: number
}

--del--

--updated--

// `An index signature parameter type must be 'string' or 'number'.`
interface IAwesomeObj {
    [key: Symbol]: number
}

--updated--

The key will eventually be string or symbol, but TS should allow us defining any types as the properties.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions