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 and dynamic properties #14359

Closed
marzelin opened this issue Feb 28, 2017 · 0 comments · Fixed by #23592
Closed

keyof and dynamic properties #14359

marzelin opened this issue Feb 28, 2017 · 0 comments · Fixed by #23592
Labels
Fixed A PR has been merged for this issue

Comments

@marzelin
Copy link

keyof works for dynamic properties where keys are strings (although not quite as expected) but not so much with numbers.

TypeScript Version: 2.2.1

Code

const getProperty = <T, P extends keyof T>(key: P, obj: T) => obj[key] 

interface Iobj {
  [key: number]: any
}

const obj: Iobj = {5: 5}
getProperty(5, obj) // Error: 'Argument of type '5' is not assignable to parameter of type 'never'.'

Expected behavior:

No errors (ts should infer that key is of type number here)

Actual behavior:

Error: 'Argument of type '5' is not assignable to parameter of type 'never'.'
@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 24, 2017
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Apr 20, 2018
@mhegazy mhegazy removed the Needs Investigation This issue needs a team member to investigate its status. label Apr 23, 2018
@mhegazy mhegazy added this to the TypeScript 2.9 milestone Apr 23, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants