Issue
I am learing TypeScripe, and I am trying to make some util ts types.
For example:
I now have an interface like this:
interface Student{
name: string,
id: number,
gender: string
}
and I am need to get the keys to form a new Array type like this
type Arr = ['name', 'id', 'gender']
so how can I write an util type to make that happen.
Solution
I think what you're looking for is this: https://stackoverflow.com/a/55128956. You can also find other solutions here https://github.com/microsoft/TypeScript/issues/13298.
In your case, you need to use the keyof
keyword to get a union of the keys (keyof Student
), and then you pass it to the TupleUnion
helper. Keep in mind that the order of the literal values in the union isn't always guaranteed.
Answered By - Pedro Durek
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.