Skip to content

Typescript should be able to derive the type of the array created by Object.keys from the key signature of the object #28284

@quinn

Description

@quinn

TypeScript Version: 3.2.0-dev.201xxxxx

Search Terms:
object.keys
Code

type Keys = 'one' | 'two'

const obj:{[key in Keys]: string} = {
  one: 'value for one',
  two: 'value for two',
}

Object.keys(obj).forEach(key =>
  console.log(obj[key])
)

Expected behavior:

No error.

Actual behavior:

$ tsc --noImplicitAny test.ts
test.ts:9:15 - error TS7017: Element implicitly has an 'any' type because type '{ one: string; two: string; }' has no index signature.

9   console.log(obj[key])
                ~~~~~~~~

Playground Link:

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions