Issue
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?
Solution
If you want to do this from scratch...
On a high level, one way of doing it is to:
Figure out how to get the
SourceFilenode using the compiler api of your file. That requires a bit of an explanation in itself.From there, use the api's
forEachChildfunction to loop over all the nodes in the file and find the node with akindofSyntaxKind.ClassDeclarationand .name property with textBar.Then loop over all the children of the class by again using the api's
forEachChildfunction and get the ones that has the kindSyntaxKind.MethodDeclarationand .name property with textfoo.To get the parameters, you will need to loop over the method node's
parametersproperty.Then for each parameter node, to get the name you can call
.getText()on the.nameproperty.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'sisOptionalParametermethod.To get its default expression, you will just have to check the
initializerproperty:propertyDeclaration.initializer;To get the type use the
TypeChecker'sgetTypeOfSymbolAtLocationmethod 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);
}
Answered By - David Sherret
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.