Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/gimenete/type-writer

Generate type definitions for TypeScript, Flow, PropTypes, etc. by using examples of the data
https://github.com/gimenete/type-writer

Last synced: 3 months ago
JSON representation

Generate type definitions for TypeScript, Flow, PropTypes, etc. by using examples of the data

Awesome Lists containing this project

README

        

# 📇 Typewriter

Generate type definitions for TypeScript, Flow, PropTypes, etc. by using examples of the data

## Installing

```
npm install @gimenete/type-writer
# or
yarn add @gimenete/type-writer
```

## Example

```javascript
const TypeWriter = require('@gimenete/type-writer')
const tw = new TypeWriter()
const examples = []
examples.push({ user: { name: 'Julia' }, foo: 'bar' })
examples.push({ user: { name: 'Julia' }, foo: 1 })
examples.push({ user: { name: 'Julia' }, foo: [1, 2, 3] })
tw.add(examples, { rootTypeName: 'ProjectName' })
console.log('# TypeScript')
console.log(tw.generate('typescript'))
console.log()
console.log('# Inlined TypeScript')
console.log(tw.generate('typescript', { inlined: true }))
console.log()
console.log('# Inlined PropTypes')
console.log(tw.generate('propTypes', { inlined: true }))
console.log()
console.log('# PropTypes')
console.log(tw.generate('propTypes'))
console.log()
```

Output:

```
# TypeScript
type ProjectNameUser = { name: string }
type ProjectName = {
user: ProjectNameUser,
foo: string | number | Array
}

# Inlined TypeScript
{
user: { name: string },
foo: string | number | Array
}

# Inlined PropTypes
PropTypes.shape({
user: PropTypes.shape({ name: PropTypes.string.isRequired }).isRequired,
foo: PropTypes.oneOfType(
PropTypes.string,
PropTypes.number,
PropTypes.arrayOf(PropTypes.number)
).isRequired
})

# PropTypes
const ProjectNameUser = PropTypes.shape({ name: PropTypes.string.isRequired })
const ProjectName = PropTypes.shape({
user: ProjectNameUser.isRequired,
foo: PropTypes.oneOfType(
PropTypes.string,
PropTypes.number,
PropTypes.arrayOf(PropTypes.number)
).isRequired
})
```