Skip to content

Mapped properties in interfaces have undefined behavior #18299

Closed
@dead-claudia

Description

@dead-claudia

TypeScript Version: Playground version

Code

Linked playground

type Foo = {
    [P in 'a' | 'b']: any;
}

interface Bar {
    [P in 'a' | 'b']: any;
}

Expected behavior:

One of these two options:

  1. No errors at all, and roughly equivalent semantics.
  2. A parse error, in case this should specifically be disallowed (to align with classes, since [foo in bar] is required to be a valid class property/method name).

Actual behavior:

A mess of type errors because [P in T]: any is parsed in interfaces as a computed symbol property with key P in T (i.e. a boolean). In kind, I get three sets of type errors:

  1. For P:

    • A computed property name in an interface must directly refer to a built-in symbol.
    • Cannot find name 'P'.
    • The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
  2. For 'a' | 'b':

    • A computed property name in an interface must directly refer to a built-in symbol.
    • The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
    • The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingFix AvailableA PR has been opened for this issueHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions