Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jtheisen opened this issue Mar 13, 2017 · 1 comment
Closed
Labels
Duplicate An existing issue was already created

Comments

@jtheisen
Copy link

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;
}
@RyanCavanaugh
Copy link
Member

See #14548

@mhegazy mhegazy added the Duplicate An existing issue was already created label Mar 13, 2017
@mhegazy mhegazy closed this as completed Apr 21, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants