Object Mapping
Zero-cost compile-time object mapping and transformation. Inspired by Scala's Chimney.
Quick Start
bash
npm install @typesugar/mappertypescript
import { transformInto } from "@typesugar/mapper";
interface User {
first_name: string;
last_name: string;
age: number;
}
interface UserDTO {
firstName: string;
lastName: string;
age: number;
role: string;
}
const user: User = { first_name: "John", last_name: "Doe", age: 30 };
const dto = transformInto<User, UserDTO>(user, {
rename: {
firstName: "first_name",
lastName: "last_name",
},
const: {
role: "user",
},
});
// dto is { firstName: "John", lastName: "Doe", age: 30, role: "user" }Features
- Zero-cost — All transformations are evaluated at compile time and inlined
- Type-safe — Compile-time validation. Fails build if a target field is not mapped
- No decorators needed — Works with plain interfaces and types
How It Works
The macro analyzes source and target types at compile time:
- Fields with matching names are copied automatically
renamemaps target field names to source field namesconstprovides constant values for fields not in the source- Missing mappings cause compile-time errors
