Git Product home page Git Product logo

just-my-type's Introduction

just_my_type

Experimental!!!

Attempts to infer a typescript type by inspecting runtime javascript objects.

Why would I want this?

  • You have a large API that is not currently typed
  • There are no/poor specifications or documentation of what the shape of the objects it returns
  • The responses it return vary, and it's difficult to know that you have a complete understanding of what it can return
  • You want to write a type for it

How to use:

The idea is that you want to run this on a real-world system, observing the real-world data for a while, then after some time has passed and you've observed many responses, you ask it to suggest a type that will match for all of them.

For each object you'd want to generate a type for, you'd create an instance of TypeGenerator then call observe, passing the data that you're wanting to generate a type for. After some time has passed and you think you have a complete understanding of what the API does, you'd call suggest and it'll make you a typescript type that fits the observed data.

Setup (node.js):

npm install @itaylor/just-my-type
import TypeGenerator from '@itaylor/just_my_type';

Setup (deno):

import TypeGenerator from 'https://deno.land/x/[email protected]/main.ts';

Usage

const tg = new TypeGenerator('MyAPI');

// Assumption: this api is being called by your application somewhere on a regular basis.
export async function myApi() {
  const data = await (await fetch('https://someserver.local/myApiThatINeedATypeFor/')).json();
  tg.observe(data);
  return data;
}
// This is a bad way to do this, but illustrates the point that `observe` should be called multiple times 
// With all the different variations that need to be observed before calling `suggest`
setInterval(() => console.log(tg.suggest()), 5 * 60 * 1000);

Every 5 minutes you'd see something like this in the logs that would be the current representation of all the data that has been observed:

export type MyAPI = {
  startIndex: number,
  maxReturn: number,
  items: Array<{
    type: string,
    children: Array<{
      x: number,
      y: number
    }>
  } | {
    type: string,
    messageText: string
  } | {
    type: string,
    responses: Array<string>
  }>
}

just-my-type's People

Contributors

itaylor avatar

Stargazers

Alex Arena avatar Chuck Dries avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.