Skip to content

for...in loop not narrowing key #14328

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

Closed
nsgundy opened this issue Feb 27, 2017 · 4 comments
Closed

for...in loop not narrowing key #14328

nsgundy opened this issue Feb 27, 2017 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@nsgundy
Copy link

nsgundy commented Feb 27, 2017

TypeScript Version: 2.2.1

Code

type AB = 'A' | 'B';

type ABMAP = {
    [P in AB]: string
}

const abMap: ABMAP = {
    'A': 'a',
    'B': 'b'
}

function log(p: AB) {
    //
}

for (const p in abMap) {
    log(p); // <<-- Argument of type 'string' is not assignable to parameter of type 'AB'.
}

Expected behavior:
p is of type AB.

Actual behavior:
p is of type string

@Jessidhia
Copy link

Any object can match your ABMAP type as long as it has at least A and B as keys. There is no exact object type declaration 😢

@nsgundy
Copy link
Author

nsgundy commented Feb 27, 2017

That's a bummer. Any other way for the log function to keep its parameter typed as AB?

@mhegazy
Copy link
Contributor

mhegazy commented Feb 27, 2017

Same explanation as in #13989

@mhegazy mhegazy added the Duplicate An existing issue was already created label Feb 27, 2017
@nsgundy
Copy link
Author

nsgundy commented Feb 28, 2017

Thanks all, closing 👍

@nsgundy nsgundy closed this as completed Feb 28, 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