TSTypeInfo
Simple TypeScript AST and code generator.
Uses the TypeScript Compiler API to get information about TypeScript code in an easy to use format.
npm install ts-type-info --save-dev
Notice - Library Deprecation!
Version 7.0 is the last final major release of ts-type-info that I will work on (feel free to still submit pull requests or bug reports).
There's certain issues with this library and to address them I had to do a complete redesign. I'm working on a more powerful library that supports more use cases: ts-simple-ast. ts-simple-ast wraps the typescript compiler rather than creating separate standalone objects. It's still a work in progress, but it is slowly getting up to speed.
AST
// V:\TestFile.ts
Get the file info:
; ; .getClass"MyClass" // get first by name .getPropertyp.defaultExpression != null; // or first by what matches console.logproperty.name; // myNumberPropertyconsole.logproperty.type.text; // numberconsole.logproperty.defaultExpression.text; // 253console.logproperty.isReadonly; // true // or access the arrays directly; console.logmyMethod.name; // myMethod
Code Generation
You can work with objects retrieved from the AST or start with your own new file definition:
; // create whatever you like at the start; // add to it later;myClass.isAbstract = true;myClass.addDecorator; myClass.addProperty;myClass.addProperty; // write it outconsole.logfile.write;
Outputs:
@MyDecoratorabstract class MyClass { myProperty1: string; myProperty2 = 4; // myMethod is here myMethod(myParam: string) { if (myParam != null && myParam.length > 40) { alert(myParam); } return myParam; }}
Simple Examples
- Strict Interfaces - Make all interface properties required and append "Strict" to the end of the interface name.
Full Examples
- TsStateTestGenerator - Code generates functions for testing the state of objects. I used this in this ts-type-info and was able to discover a bunch of unreported bugs. I also no longer have to maintain a large portion of the project because the code is automatically generated for me. I highly recommend this.
- TsCloneableGenerator - Code generates functions for cloning and filling objects.
- TsObjectCreate - Code generates functions for creating objects with their types.
- Server Bridge - Automatically generates client side code to communicate with the server from the server side code.
Include tsNodes
In case there's something you need from the compiler that's not implemented in this library, set the includeTsNodes
option to true.
This will include the TypeScript compiler nodes in the tsNode
property of most objects.
;; ;; // ts.TypeChecker in case you need it;; console.logmyMethodNode.body.statements.getText; // "return `Test: ${myParameter}`;"