An open API service indexing awesome lists of open source software.

https://github.com/chuanqisun/torex

Typed Object Reflection: Infer TypeScript interface from JSON
https://github.com/chuanqisun/torex

Last synced: 8 months ago
JSON representation

Typed Object Reflection: Infer TypeScript interface from JSON

Awesome Lists containing this project

README

          

# 🦖 Torex

**Typed Object Reflection**: Infer TypeScript interface from JSON

## Get started

Install

```bash
npm i torex
```

Examples

```typescript
import { getType } from "torex";

getType({
myKey: "myValue",
})
/*
interface IRoot {
myKey: string;
}
*/

getType({ myKey: "myValue" }, { rootName: "MyObject" });
/*
interface IMyObject {
myKey: string;
}
*/

getType([{ name: "a" }, { name: "b", size: 42 }]);
/*
type Root = IRootItem[];

interface IRootItem {
name: string;
size?: number;
}
*/

getType([{ name: "a" }, { name: "b", size: 42 }], { scope: "root-item" });
/*
type Root = IRootItem[];

interface IRootItem {
name: string;
size?: number;
}
*/

getType([{ name: "a" }, { name: "b", size: 42 }], { rootName: "MyObject", scope: "root-item" }));
/*
interface IMyObject {
name: string;
size?: number;
}
*/
```