Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/gimenete/type-writer
- Owner: gimenete
- License: mit
- Created: 2018-07-17T10:43:31.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T07:50:02.000Z (about 2 years ago)
- Last Synced: 2024-10-04T13:39:10.670Z (4 months ago)
- Language: JavaScript
- Size: 396 KB
- Stars: 17
- Watchers: 1
- Forks: 1
- Open Issues: 18
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
})
```