Nonviolent Pigeon Manifestation

    @n1kk/intspector

    1.0.2 • Public • Published

    Inpector

    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 🐈.

    So thanks paulkoerbitz ❤️

    License

    This project is licensed under MIT.

    Install

    npm i @n1kk/intspector

    DownloadsWeekly Downloads

    7

    Version

    1.0.2

    License

    MIT

    Unpacked Size

    36.6 kB

    Total Files

    22

    Last publish

    Collaborators

    • n1kk