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

resolve-types

v0.2.0

Published

Resolve TypeScript types from inline code - useful for writing type-level tests

Downloads

8

Readme

resolve-types

npm version

resolve-types allows you to resolve types in inline code. This functionality is handy to write unit tests for type operators and check them in a regular test suite.

Compatibility Note

The shape of the return type of resolveTypes changed in version 0.2.0 to return diagnostic errors. It now returns a value of shape { diagnostics, types } whereas it returned the content of types directly in versions 0.1.*.

Example

resolve-types can be used as follows to test type operators:

import "jest"; // example test runner
import { resolveTypes } from "resolve-types";

describe("Type Operators Example", () => {
    it("Pick extracts keys from objects", () => {
        const code = "\
            type __1 = Pick<{ a: number; b: string; c: any; }, 'a' | 'b'>;\
        ";
        const { types: { __1 } } = resolveTypes(code);
        expect(__1).to.equal("{ a: number; b: string; }");
    });

    it("resolveTypes also works with template literals", () => {
        const { types: { __2 } } = resolveTypes`
            type ${1} = Pick<{ a: number; b: string; c: any; }, 'a' | 'b'>;
            type ${2} = Pick<${1}, 'a'>;
        `;
        expect(__2).to.equal("{ a: number; }");
    });

    it("resolveTypes returns diagnostic messages in the 'diagnostics' property ", () => {
        const { diagnostics, types } = resolveTypes`
            import * as foo from 'i-dont-exist';
        `;
        expect(diagnostics[0].code).toEqual(2307);
        expect(diagnostics[0].messageText).toEqual("Cannot find module 'i-dont-exist'.");
    });
});

Documentation

The resolve-types library exposes two functions: resolveTypes which does the actual work and setOptions which allows setting options for the TypeScript compilation.

resolveTypes

resolveTypes takes either a string or a template literal, creates a TypeScript program out of it and extracts the types of specially named type declarations and returns compliation diagnostics and the resolved types. Type type declarations must have the form type __[a-zA-Z0-9][_a-zA-Z0-9]*. In template literals, any expressions which are resolved are automatically prefixed with __ to fulfill the naming requirement.

The return value of resolveTypes has the shape

{
    types: { [key: string]: string; };
    diagnostics: ts.Diagnostic[];
}

where ts.Diagnostic is the type of Diagnostics from the TypeScript compiler. Both types and diagnostics are computed lazily to minimize the performance impact of objects which are never read.

resolveTypes may return "<unknown>" for any types it cannot resolve. This happens especially for compilation errors, check the diagnostics property for these.

setOptions

setOptions allows setting TypeScript compiler options before compiling the program with resolveTypes. By default, the options from the project's tsconfig.json will be used. To override this, set setOptions second parameter to true.

setOptions sets a global options variable, so its use is stateful and will be maintained across invocations of resolveTypes.

Contributing

Contributions in the form of bug reports, change requests, documentation and code changes are welcome. Please make sure there is an outstanding ticket which has been discussed before making large code changes.

License

MIT