Tech News
← Back to articles

Comptime.ts: compile-time expressions for TypeScript

read original related products more articles

⚡️ comptime.ts

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.

Contents

What is comptime.ts?

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.

Examples

1. Simple sum function

import { sum } from "./sum.ts" with { type: "comptime" }; console.log(sum(1, 2));

... continue reading