ts2nim
raw JSON → 0.0.31 verified Fri May 01 auth: no javascript
A TypeScript to Nim transpiler that converts TypeScript source code (and JavaScript via TypeScript ESTree) into valid, pretty Nim code. The 0.0.31 release is currently in early development with limited semantic analysis; it focuses on syntax translation and basic type mapping (e.g., number→float, Array→seq, null→nil). The tool also generates wrapper code from .d.ts files using importcpp pragmas. Compared to alternatives like dts2nim, ts2nim directly translates full TypeScript sources rather than just type definitions. It is intended to expand Nim's JavaScript backend ecosystem by reusing existing TypeScript/JavaScript libraries. Note that the project does not resolve dependencies and requires manual post-translation adjustments.
Common errors
error Error: Cannot find module 'ts2nim' ↓
cause The package is not installed, or the import path is incorrect.
fix
Run npm install ts2nim and use correct import: import ts2nim from 'ts2nim'.
error TypeError: ts2nim.transpile is not a function ↓
cause Incorrect import; transpile is a method on the default export, not a named export.
fix
Use import ts2nim from 'ts2nim' and call ts2nim.transpile().
error SyntaxError: Unexpected token ':' - expected '(' in Nim code ↓
cause TypeScript type annotations are not fully removed or converted incorrectly for certain constructs.
fix
Manually review and fix the generated Nim code, especially around function signatures and type casts.
error Error: TSMethodSignature not supported in current mode ↓
cause Using .d.ts files requires mode set to 'declaration' or specific options.
fix
In the CLI, use --declaration flag or set mode: 'declaration' in programmatic API.
Warnings
gotcha The transpiler does not resolve external dependencies; it only translates the given source files. ↓
fix Manually import and translate required modules, or use .d.ts files for wrapper generation.
gotcha Type inference for native types (e.g., number as int vs float) is not yet implemented; all numbers become float. ↓
fix Manually adjust number types in generated Nim code.
gotcha Multiple inheritance from TypeScript classes is not supported; Nim does not support multiple inheritance. ↓
fix Refactor TypeScript classes to avoid multiple inheritance, or use composition instead.
gotcha Rest parameters in TypeScript become openArray[T] in Nim, which may require manual adjustment. ↓
fix Review generated Nim code for rest parameters and adjust types as needed.
gotcha Async functions are translated with {.async.} pragma but require the asyncdispatch or asyncjs module to be imported manually. ↓
fix Add import asyncdispatch or import asyncjs to the top of generated Nim file.
Install
npm install ts2nim yarn add ts2nim pnpm add ts2nim Imports
- default
import ts2nim from 'ts2nim' - CLI
import { cli } from 'ts2nim' - Type
import type { TranspileOptions } from 'ts2nim'
Quickstart
import ts2nim from 'ts2nim';
import { readFileSync, writeFileSync } from 'fs';
const typescriptCode = `
function greet(name: string): string {
return 'Hello, ' + name;
}
`;
ts2nim.transpile(typescriptCode, { mode: 'source' }).then(result => {
writeFileSync('greet.nim', result.output);
console.log('Transpiled successfully');
});