ts_generator 0.1.0

Automatic TypeScript interface and enum code generator from D types using compile-time reflection (traits & CTFE).


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:


This package provides sub packages which can be used individually:

ts_generator:example - Example application demonstrating ts_generator usage

ts_generator

A lightweight, zero-dependency D library for automatically generating TypeScript interface and enum definitions from D struct, class, and enum types using compile-time reflection (__traits and CTFE).

DUB Package License: MIT

Features

  • Compile-Time Generation: Generates TypeScript code entirely at compile time using D traits and CTFE.
  • Recursive Type Discovery: Automatically finds and emits nested structs, classes, and enums referenced in your fields.
  • D Enum Support: Handles integer enums (export enum ...) and custom string enums (export enum ...).
  • Nullable Support: Translates std.typecons.Nullable!T fields to TypeScript union type T | null.
  • Date & Time: Automatically maps std.datetime types (SysTime, DateTime, Date) to ISO string representations (string).
  • Collections: Supports single & multi-dimensional dynamic/static arrays (T[]) and associative arrays ({ [key: string]: Value }).
  • Clean Field Reflection: Uses UnqualT.tupleof to only reflect instance data fields, ignoring methods, constructors, constants, and static members.

Installation

Add ts_generator to your dub.json:

"dependencies": {
    "ts_generator": "~>1.0.0"
}

Or run:

dub add ts_generator

Quick Example

1. Define D Types and Export

import std.file : write, mkdirRecurse;
import std.path : dirName;
import ts_generator;

enum UserRole {
    admin,
    user,
    guest
}

enum StrEnum : string {
    first = "FIRST_VAL",
    second = "SECOND_VAL"
}

struct UserProfile {
    string nickname;
    int age;
    UserRole role;
}

struct ServerResponse {
    uint statusCode;
    string message;
    UserProfile user;
    UserProfile[] history;
    StrEnum strKind;
}

void main()
{
    // Generate TypeScript type declarations
    string ts = generateTypeScript!(ServerResponse)();

    // Export to frontend file
    mkdirRecurse("frontend/src/types");
    write("frontend/src/types/api.d.ts", ts);
}

2. Output (frontend/src/types/api.d.ts)

export enum UserRole {
    admin = 0,
    user = 1,
    guest = 2,
}

export interface UserProfile {
    nickname: string;
    age: number;
    role: UserRole;
}

export enum StrEnum {
    first = "FIRST_VAL",
    second = "SECOND_VAL",
}

export interface ServerResponse {
    statusCode: number;
    message: string;
    user: UserProfile;
    history: UserProfile[];
    strKind: StrEnum;
}

Supported Type Mapping

D TypeTypeScript Equivalent
string, wstring, dstring, charstring
boolboolean
int, uint, long, float, double, etc.number
SysTime, DateTime, Datestring
Nullable!T`T \null`
T[]T[]
V[K]{ [key: string]: V }
enum (numeric)export enum EnumName { member = 0, ... }
enum (string)export enum EnumName { member = "val", ... }
struct, classexport interface Name { ... }

Running Tests & Examples

To run unit tests:

dub test

To run the included example app:

dub run :example

Publishing to DUB Registry

To publish new releases to code.dlang.org:

  1. Tag the release in git:
   git tag v1.0.0
   git push origin v1.0.0
  1. Register the repository at https://code.dlang.org/publish.

License

MIT © Pablo De Nápoli

Authors:
  • Pablo De Nápoli
Sub packages:
ts_generator:example
Dependencies:
none
Versions:
0.2.0 2026-Sep-01
0.1.0 2026-Sep-01
~main 2026-Sep-01
Show all 3 versions
Download Stats:
  • 1 downloads today

  • 1 downloads this week

  • 1 downloads this month

  • 1 downloads total

Score:
0.0
Short URL:
ts_generator.dub.pm