npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@itaylor/just_my_type

v0.0.6

Published

Derives typescript types from JS objects

Downloads

11

Readme

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>
  }>
}