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

intspector

v1.0.2

Published

Inspect your TS types as strings and integrate type testing into your favorite testing framework!

Downloads

3

Readme

Build Status Npm Coverage Maintainability Dependency Status Dev Dependency Status vump

Inspect your TS types as strings and integrate type testing into your favorite testing framework!

Motivation

When you find yourself writing a library with complex typings, you might find the urge to test your meta types, return types of your function etc.

The usual approach is an assignment to a typed variable, which is a good start. But (1) it is not a test for an exact type match, but for one-way type bound in type hierarchy due to inheritance, and (2) your unit test now either passes or does not compile (in case you break the typings), which is not exactly a way to go, sir.

When you are really into it, you set up dtslint. There are lot of disadvantages from my point of view. See See also section lower. But if you take it seriously, the main disadvantage is that you have to spell out all the type annotations by yourself. Which means you cannot take advantage of Jest's snapshot testing for instance, or any other tools you might want to use in testing.

This project comes to rescue with TypeScript types to string serialization using TypeScript compiler API.

Getting started

  1. Install
npm install intspector
  1. Start using with your project's tsconfig.json!
import { inspect } from 'intspector';

inspect('typeof Math.random()') // "number"

API

See full public API typedoc documentation.

Inspect functions

inspect inspects a single type or multiple types if provided an object

inspect('Record<keyof any, Array<number>>'); // "{ [x: string]: number[]; [x: number]: number[]; };"
inspect({a: 'string | number', b: 'boolean'}); // {a: "string | number", b: "boolean"}

inspectWithPreamble HOF returning inspect with code to be executed beforehand. The inspect is still the same function: can take object or string.

const inspectWithFoo = inspectWithPreamble('const foo = 2');
inspectWithFoo('typeof foo'); // "2"
inspectWithFoo({ foo: 'typeof foo' }); // { foo: "2" }

Options functions

getOptions to see what you are working with

const opts = getOptions();
// opts contain current options

setOptions to override your project defaults

import * as ts from 'typescript';

const opts = setOptions({ strict: false, module: ts.ModuleKind.CommonJS });
// opts contain current options

Advanced usage

You can self-reference your types

const res = inspect({
    a: 'string',
    b: 'a | Promise<a>',
});
// res: { a: "string", b: "string | Promise<string>" }

You can import your types and all imports, or even put any amount of valid code into preamble.

const myInspect = inspectWithPreamble(`
import { foo } from 'bar'; // foo = 1 + 1
const ans = () => 42;
const ran = 'foo'.toUpperCase();
type Complex = { r: number; i: number };`);
const res = myInspect({
    foo: 'typeof foo',
    ansRes: 'ReturnType<typeof ans>',
    typeOfRan: 'typeof ran',
    real: "Pick<Complex, 'r'>",
});
// res: { foo: "number", ansRes: 'number', typeOfRan: 'string', real: '{ r: number; }' }

See also

  • resolve-types - Forked from. Has some bugs fixed in this project and slightly different API. Worth cheking out.
  • dtslint - This is kind of a standard when testing types - it's from Microsoft and is widely used. However it uses Lint as the test runner, which is obscure. I found it kind of difficult to setup and integrate to a project and it uses TSLint, which is going to be deprecated in 2019.

Credits

This project is a fork of resolve-types. After using the original project I reported some issues (#1, #2, #3), but got no reply. The project was too special and too good to let go. So I created this fork to fix some of the issues and make the API easier to use.

Apart from the fixes, I changed a lot, but still use some of the core functions to operate the Typescript compiler and thanks to the original project I learned how to cat :cat2:.

So thanks paulkoerbitz :heart:

License

This project is licensed under MIT.