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).

Share Improve this question edited Sep 25, 2018 at 15:37 MaYaN asked Sep 25, 2018 at 15:28 MaYaNMaYaN 6,99613 gold badges60 silver badges118 bronze badges 5
  • 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
Add a ment  | 

5 Answers 5

Reset to default 8

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, [])))

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