Skip to content

The simple way to resolve/infer TypeScript types. Useful to type-aware linting in Rust.

License

Notifications You must be signed in to change notification settings

kermanx/simple_ts

Repository files navigation

simple_ts

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.

Why?

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.

Goals

  • Type inference implemented within 10k LOC
  • Support oxc's Type Aware Linting
  • Fast enough
  • (Possibly) Fast DTS emitter without the requirement of isolatedDeclarations

Non-goals

  • Type checking
  • Type hinting/LSP
  • 100% TypeScript compatibility (In fact, many TypeScript behaviors are never documented)

Status

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

A small demo

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.

About

The simple way to resolve/infer TypeScript types. Useful to type-aware linting in Rust.

Topics

Resources

License

Stars

Watchers

Forks

Languages