JSON to TypeScript Class Generator Online
Auto-generate TypeScript interfaces from any JSON โ nested objects ยท optional fields ยท readonly ยท export ยท 100% Free
export interface User {
id: number;
name: string;
active: boolean;
address: Address;
}TypeScript classes provide more functionality than interfaces โ they can have methods, implement interfaces, use decorators and be instantiated. This class generator creates TypeScript classes from JSON with properly typed properties, a typed constructor and optional decorator support for Angular, NestJS or class-validator integration. Get class definitions ready for your OOP TypeScript project.
What is JSON to TypeScript Interface Generation?
This tool analyses the structure of your JSON data and automatically generates TypeScript interface or type declarations โ saving you the time of writing them manually. It infers string, number, boolean, null, nested object interfaces, and typed arrays. When your JSON is an array of objects, optional fields are detected automatically.
How Nested Objects Are Handled
Every nested JSON object generates its own named TypeScript interface. For example, if your JSON has "address": { "city": "Ahmedabad" }, the tool generates a separate Address interface and references it as address: Address in the parent interface โ matching real-world TypeScript project structure.
Frequently Asked Questions
When should I use a TypeScript class vs interface for JSON data?
Use class when: you need methods (getFullName(), isActive()), using Angular services with dependency injection, using NestJS DTOs with class-validator decorators, or need instanceof checks. Use interface when: just defining data shape, working with plain objects, writing library types, or when bundle size matters (interfaces compile to nothing). For API response types, interfaces are usually sufficient.
How do I add class-validator decorators to generated TypeScript classes?
Add decorators to each property: @IsString() name: string; @IsNumber() age: number; @IsEmail() email: string; @IsBoolean() active: boolean; @IsArray() @IsString({ each: true }) tags: string[];. Install: npm install class-validator class-transformer. Use ValidateAsync(instance) to validate at runtime. Common in NestJS controllers and DTOs.
How do I use class-transformer with generated TypeScript classes?
class-transformer converts plain JSON to class instances: const user = plainToClass(User, jsonData). Now user is a User instance with methods. Combine with @Expose() and @Exclude() decorators to control serialization. @Transform() for value transformation. Angular HTTP: return this.http.get("/api/user").pipe(map(data => plainToClass(User, data))).
Can TypeScript classes implement multiple interfaces?
Yes: class User implements IUser, Serializable, Validatable { ... }. The class must implement all methods and properties from all interfaces. This is useful for ensuring classes follow contracts from multiple sources. Unlike Java, TypeScript uses structural typing โ a class automatically satisfies an interface if it has all required properties, even without explicit implements.