Skip to content

keyof types don't appear to be subtypes of string when used in mapped types #14625

Closed
@jtheisen

Description

@jtheisen

TypeScript Version: 2.2.0

Code

function foo<T>() {
    let x: { [P in keyof T]: number }
    let y: { [k: string]: number } = x;
}

Expected behavior:

The assigment should be allowed.

Actual behavior:

The assignment isn't allowed.

This is despite this variant working just fine:

let x: {[P in 'one' | 'two']: number } = null;
let y: { [k: string]: number } = x;

And this works too, as just as the documentation states, keyof T is a subtype of string for all T:

function foo<T>() {
    let kt: keyof T = '' as keyof T;
    let sn: string = kt;
}

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