Skip to content

Type checker doesn't understand string literal type in computed property keyย #15534

Closed
@whatisaphone

Description

@whatisaphone

TypeScript Version: nightly (2.4.0-dev.20170502)

Code

interface Thing {
  readonly foo: number;
}

function bar(t: Thing) { }

function qux(key: 'foo', value: number) {
  bar({ foo: 5 });  // works
  bar({ [key]: 5 });  // doesn't work
}

Expected behavior:

Both calls typecheck.

Actual behavior:

index.ts(9,7): error TS2345: Argument of type '{ [x: string]: number; }' is not assignable to parameter of type 'Thing'.
  Property 'foo' is missing in type '{ [x: string]: number; }'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions