https://github.com/jlkiri/tsx-ray
Extract interfaces as Javascript objects from Typescript files. JSON-compatible.
https://github.com/jlkiri/tsx-ray
Last synced: 2 months ago
JSON representation
Extract interfaces as Javascript objects from Typescript files. JSON-compatible.
- Host: GitHub
- URL: https://github.com/jlkiri/tsx-ray
- Owner: jlkiri
- License: mit
- Created: 2020-03-28T12:54:05.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T18:15:12.000Z (over 2 years ago)
- Last Synced: 2025-04-14T21:13:44.195Z (2 months ago)
- Language: TypeScript
- Homepage:
- Size: 1.78 MB
- Stars: 9
- Watchers: 2
- Forks: 2
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Intro
This library extracts interface definitions from a TypeScript source file and parses it into a JSON-compatible JavaScript object.
Use it to turn this
```typescript
interface Attendee {
id: ID;
person: Person;
accompaniedBy?: Person;
status: 'beginner' | 'experienced' | 'pro';
accessLv: 1 | 2 | 3;
}interface Person {
name: string;
isUnderage: boolean;
address: Address;
phoneNumbers: PhoneNumbers;
}interface Address {
city: City | CityCode;
country: Country;
}type Country = string;
type City = string;
type CityCode = number;
type ID = string;
type PhoneNumbers = string[];
```into this
```javascript
{
Address: {
city: ['string', 'number'],
country: 'string',
},
Attendee: {
id: 'string',
person: {
name: 'string',
isUnderage: 'boolean',
phoneNumbers: ['string'],
address: {
city: ['string', 'number'],
country: 'string',
},
},
accompaniedBy: {
name: 'string',
isUnderage: 'boolean',
phoneNumbers: ['string'],
address: {
city: ['string', 'number'],
country: 'string',
},
},
status: ['beginner', 'experienced', 'pro'],
accessLv: [1, 2, 3],
},
Person: {
name: 'string',
isUnderage: 'boolean',
phoneNumbers: ['string'],
address: {
city: ['string', 'number'],
country: 'string',
},
},
}
```## Usage
The exported function `extractInterfacesFromFile` takes a filepath argument:
```javascript
import { extractInterfacesFromFile } from 'tsx-ray';const result = extractInterfacesFromFile('src/mytsfile.ts');
console.log(result);
/* Person: {
name: 'string',
isUnderage: 'boolean',
phoneNumbers: ['string'],
address: {
city: ['string', 'number'],
country: 'string',
},
} */
```