admin管理员组文章数量:1241093
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)
.
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)
.
- where is the array defined? Is each one an array or is there a different array property? How do you want to use this? – Get Off My Lawn Commented Sep 25, 2018 at 15:35
- There is no array, I want to be able to produce an array based on the keys (fields) defined on the type. – MaYaN Commented Sep 25, 2018 at 15:36
- Okay thanks for clarifying! – Get Off My Lawn Commented Sep 25, 2018 at 15:37
- @MaYaN Could you please explain your post in more detail with example? I am not able to understand it currently. – vibhor1997a Commented Sep 25, 2018 at 15:44
- I don't think you can get it from the types. – vibhor1997a Commented Sep 25, 2018 at 15:49
5 Answers
Reset to default 8You 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, [])))
If you just one to list the field's name from a class you can use the following:
let fields = Object.keys(myObj) as Array<MyClass>;
Hope it helps.
Since the code above will be transpiled to following js code:
var Foo = (function () {
function Foo(one, two, three) {
this.one = one;
this.two = two;
this.three = three;
}
return Foo;
}());
I'd first extract the constructor using const c = Foo.prototype.constructor
and the get the name of arguments from that. This thread shows how to do that.
You need to instantiate const a = new Foo();
and access using Object.keys
class Foo {
constructor(
private one: string,
private two: string,
private three: string) {
}
}
const a = new Foo();
console.log(Object.keys(a)); // ["prop"]
DEMO
EDIT: If you want to get from the type it is not possible since types won't be available once the code is piled.
Scan the Object.keys(Foo.prototype)
本文标签: javascriptHow can I get properties of a type as an array in TypescriptStack Overflow
版权声明:本文标题:javascript - How can I get properties of a type as an array in Typescript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740049752a2222085.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论