samedi 9 janvier 2016

Typescript reflection - required parameters & default values

In short: is there a way to know if a typescript parameter is required and/or has a default value?

Longer version: Say I have the following file:

//Foo.ts
class Bar {
    foo(required:string,defaultValue:number=0,optional?:boolean) {
        ...
    }
}

I would like to know of each of the parameters:

  • the name
  • the type
  • is it required?
  • does it have a default value?

I have succesfully used method decorators with the TypeScript reflection API to get the types of the parameters, I've used this method to get their names, but so far I have not found a way to know if a variable is required and/or has a default value.

I know the typescript compiler itself can be used from within typescript. So I'm wondering if there is a way to use the parse tree of the compiler to see if a parameter is required and/or has a default value?

How would that work?





Aucun commentaire:

Enregistrer un commentaire