Typescript: keyof as object keys

Created on 9 Oct 2017  ·  1Comment  ·  Source: microsoft/TypeScript

is it possible to set something like this with typescript?

interface SomethingElse {}

type Fruits = {
    banana: boolean,
    apple: boolean,
    melon: boolean,
    //.... 1000 records
    grape: boolean
}

const list: { [key: keyof fruits]: SomethingElse }

what i am trying to archive here is reuse the keys from fruits to create a new type that is no longer boolean, as you see i could create a new interface/type where i could assign the type as SomethingElse however that means i would need to duplicate all the fruits keys........

would it fit in a feature request? or you guys think this is non sense?

External

Most helpful comment

Seems like i missed the in as in const list: { [key in keyof fruits]: SomethingElse }

>All comments

Seems like i missed the in as in const list: { [key in keyof fruits]: SomethingElse }

Was this page helpful?
0 / 5 - 0 ratings