镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

keyof and dynamic properties #14359

Description

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'.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions