The simple way to resolve/infer TypeScript types. Written in Rust.
Note
This project already implemented the key features but it is far from usable. See Status.
There have been several tries to implement a TypeScript type checker in Rust. Why another one?
The main reason is that they are just too complex. For example, stc takes 2.4k LOC to check BinaryExpression
.
I am working on tree-shaker, and I find its pattern can be used to infer TypeScript types. And after reading Rust-Based JavaScript Linters: Fast, But No Typed Linting Right Now by Josh Goldberg, I came up with the idea of copying some of tree-shaker's code and make a TypeScript type inferrer. To avoid the complexity, I will not implement type checking, but only type inference. See the goals and non-goals below.
- Type inference implemented within 10k LOC
- Support oxc's Type Aware Linting
- Fast enough
- (Possibly) Fast DTS emitter without the requirement of
isolatedDeclarations
- Type checking
- Type hinting/LSP
- 100% TypeScript compatibility (In fact, many TypeScript behaviors are never documented)
Currently, the project is of 6k LOC.
The following TypeScript features are implemented/to be implemented:
- Basic type inference
- Union and intersection type
- Generic type, Object type, and Tuple type
- Conditional type and inference:
T extends infer U ? X : Y
- Control-flow-based type inference
- (Partial) Generic function and inference
- (Partial) Interface type
- (Partial) Printing types
- Type narrowing/guards/assertions
- Enum type
- Class
- Builtin libs
- Multiple files support
For AST nodes, some are implemented and some are not.
As you can see, the hardest part (generic and its inference and control flow analysis) has already been implemented. The rest is relatively easy.
However, the remaining part still requires a lot of time. I am afraid that I don't have enough spare time to work on this. If there are kind people who want to realize it, pwease take over this project. Thank you.