Skip to content

Object.keys does not return strongly typed keys in es6 #15627

Closed
@npenin

Description

@npenin

TypeScript Version: 2.3.2
Code

export function array<T>(array: T[], body: (element: T, i: number) => void)
{
    Array.prototype.forEach.call(array, body);
}

export function object<T>(o: T, body: <U extends keyof T>(element: T[U], i: U) => void)
{
    array(Object.keys(o), function (key, i)
    {
        body(o[key], key);
    });
}

Expected behavior:
No compile error

Actual behavior:
Compile error

severity: 'Error'
message: 'Argument of type 'string' is not assignable to parameter of type 'keyof T'.'
at: '10,22'
source: 'ts'

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