A dead-simple TypeScript compiler that does one thing really well: enables compile-time evaluation of expressions marked with comptime. This is useful

comptime.ts — compile-time expressions for TypeScript

submited by
Style Pass
2025-08-03 19:30:05

A dead-simple TypeScript compiler that does one thing really well: enables compile-time evaluation of expressions marked with comptime.

This is useful for optimising your code by moving computations from runtime to compile time. This project was inspired by Bun macros and Zig comptime (hence the name).

Warning: You are responsible for ensuring that the expressions you mark with comptime are safe to evaluate at compile time. comptime.ts does not perform any isolation. However, comptime imports are only allowed in project files, and not in node_modules. You may however import from node_modules as comptime.

comptime.ts allows you to evaluate expressions at compile time, similar to compile-time macros in other languages. This can help optimise your code by moving computations from runtime to compile time.

Note: The @emotion/css import got removed from the output. You'll need to somehow add the styles back to your project somehow. See running code after comptime evaluation for an example of emitting the styles as a CSS file. Alternatively, you might write a bundler plugin to import the CSS cache from @emotion/css and emit them as a CSS file, etc.

Leave a Comment
Related Posts