Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 566
Flion Avatar asked Jan 09 '16 18:01

Flion


People also ask

Does typescript have reflection?

Summary. The reality is that Typescript Reflection is very poor. In order to get a very basic set of Reflection features — we need to make a number of hacky solutions. Other than Dependency Injection tools, I see no other use for this limited functionality.

Why are optional parameters are added?

Optional Parameters are parameters that can be specified, but are not required. This allows for functions that are more customizable, without requiring parameters that many users will not need.


1 Answers

If you want to do this from scratch...

On a high level, one way of doing it is to:

  1. Figure out how to get the SourceFile node using the compiler api of your file. That requires a bit of an explanation in itself.

  2. From there, use the api's forEachChild function to loop over all the nodes in the file and find the node with a kind of SyntaxKind.ClassDeclaration and .name property with text Bar.

  3. Then loop over all the children of the class by again using the api's forEachChild function and get the ones that has the kind SyntaxKind.MethodDeclaration and .name property with text foo.

  4. To get the parameters, you will need to loop over the method node's parameters property.

  5. Then for each parameter node, to get the name you can call .getText() on the .name property.

  6. You can tell if the parameter is optional by doing:

     const parameterDeclaration = parameterNode as ts.ParameterDeclaration;
     const isOptional = parameterDeclaration.questionToken != null || parameterDeclaration.initializer != null || parameterDeclaration.dotDotDotToken != null;
    

    Or you could use the TypeChecker's isOptionalParameter method.

  7. To get its default expression, you will just have to check the initializer property:

     propertyDeclaration.initializer;
    
  8. To get the type use the TypeChecker's getTypeOfSymbolAtLocation method and pass in the symbol of the node... that gets a little bit complicated so I won't bother explaining it (think about how it's different with union types and such).

Don't do it from scratch...

I've created a wrapper around the TypeScript compiler api. Just use this code with ts-simple-ast (edit: Previously this talked about my old ts-type-info library, but ts-simple-ast is much better):

import { Project } from "ts-morph";

// read more about setup here:
// https://ts-morph.com/setup/adding-source-files
const project = new Project({ tsConfigFilePath: "tsconfig.json" });
const sourceFile = project.getSourceFileOrThrow("src/Foo.ts");
const method = sourceFile.getClassOrThrow("Bar").getInstanceMethodOrThrow("foo");

Once you have the method, it's very straightforward to get all the information you need from its parameters:

console.log(method.getName()); // foo

for (const param of method.getParameters()) {
    console.log(param.getName());
    console.log(param.getType().getText());
    console.log(param.isOptional());
    console.log(param.getInitializer() != null);
}
like image 181
David Sherret Avatar answered Oct 15 '22 13:10

David Sherret