keyof T doesn't appear as a string subtype within mapped types

81 views Asked by At

The following snippet fails on the assignment of y due to incompatible types:

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

However, the documentation states:

A keyof T type is considered a subtype of string.

If that was true, why does the assignment fail?

Assigning a keyof T to a string works, and assigning a { [P in 'foo']: number } to { [P in string]: number } also works.

0

There are 0 answers