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

https://github.com/monolithed/json-to-typescript

Converts a JSON object to TypeScript interfaces
https://github.com/monolithed/json-to-typescript

dts json typescript typings

Last synced: 6 months ago
JSON representation

Converts a JSON object to TypeScript interfaces

Awesome Lists containing this project

README

          

# json-to-typescript

> Converts a JSON object to TypeScript interfaces

### Installation

```
npm install json-to-typescript --save
```

### Interface

```typescript
type Predicate = (
value: JSONValue,
key: string,
target: Target
) => boolean;

declare function transform (
name: string,
json: Target,
filter?: Predicate
): Promise;
```

### Usage

```typescript
import { transform } from 'json-to-typescript';

const json = {
'x': 0,
'x-1': '1',
'y-2': '2'
};

transform('X', json);
```

**Output**

```typescript
export interface X {
x?: number;
"x-1"?: string;
"y-2": string
[x: string]: any;
}
```

#### Filter

Use the last param to filter your properties:

```typescript
import { transform } from 'json-to-typescript';

const json = {
'x': 0,
'x-1': '1',
'y-2': '2'
};

const filter = (value: any) => value !== '2';

transform('X', json, filter);
```

**Output**

```typescript
export interface X {
x?: number;
"x-1"?: string;
}
```

### Contributing

Feel free to submit a pull request if you find any bugs.

Please make sure all commits are properly documented.

### Tests

```
npm test
```

### License

MIT