Issue
Given the following type:
class Foo {
constructor(
private one: string,
private two: string,
private three: string) {
}
}
How can I have an array whose values are the type's properties?
e.g. I need to be able to get an array as:
['One', 'Two', 'Three']
Note I need to have the properties extracted from a type and not an instance otherwise I could simply use Object.keys(instanceOfFoo)
.
Solution
You can use Reflect.construct()
to get the keys, then use Object.keys()
to convert that to an array.
Note: if the key doesn't have a default it won't be generated as you can see with four
.
class Foo {
constructor() {
this.one = ''
this.two = ''
this.three = ''
this.four
}
}
console.log(Object.keys(Reflect.construct(Foo, [])))
Answered By - Get Off My Lawn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.