extract-typedefs
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

extract-typedefs

Extracts Flow or TypeScript type definitions of sample JSON values.

npm

Usage

 
const extractTypedefs = require('./index.js')
 
//Create an object with sample values for your types
const sampleData/*: {person: Person, relation: Relation} */= {
    person: {
        id:10212,
        name:'John',
        age: 22,
        parents: [
            {
                name: 'Jane',
                age: 45,
            },
            {
                name: 'Tim',
                age: 45,
            }
        ]
    },
    relation: {
        r1:10210,
        r2:12102,
        relationType: 'Friends'
    }
}
 
 
//Save the input of this function in a file:
console.log(extractTypedefs.flow(sampleData))
 
//Require the file
/*::
import type {Person, Relation} from './types.js'
*/
 
 
//Voila, a typed JSON API:
const me /*:Person*/ = {
    id:111, 
    name:'Boris',
    age:'22', //Throws type error
    parents: []
}
 

Future Development Ideas

  • Add a CLI part which works with URL endpoints.

Package Sidebar

Install

npm i extract-typedefs

Weekly Downloads

2

Version

2.0.1

License

ISC

Last publish

Collaborators

  • boris_m