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

expand-my-type

v0.6.7

Published

Expand TypeScript type expressions programmatically

Downloads

34

Readme

Expand My Type

In TypeScript, expanding a type (also known as computing, resolving, simplifying, unpacking, or unwrapping) refers to converting a type expression into a flattened type that doesn't reference other types. This is mostly done to improve the readability of type hints shown in editors/IDEs.

Expand My Type provides a programmatic way to expand type expressions in TypeScript (see How It Works section for more information). It gets the source code as input, along with a type expression and returns the expanded form as a string. That can be useful for code-generation, testing, and debugging complex type errors.

Under the hood, it uses the TypeScript Compiler API to expand the type expression and optionally formats the output using Prettier.

CLI

Installation

To install the CLI globally, run:

npm install -g expand-my-type

Alternatively, the CLI can be run using npx:

npx expand-my-type

CLI Usage

Usage:
  expand-my-type [options] <source-file> <expression>

Options:
  -h, --help                    Show this help message
  -p, --prettify                Prettify the output (default)
  -P, --no-prettify             Do not prettify the output
  -c, --tsconfig <file>         Use the specified tsconfig.json file

Given a file named example.ts with the following contents:

interface SomeInterface {
  a: number;
  b?: string;
  c: number | undefined;
}

type SomeType = {
  d: number;
  e: SomeInterface;
  f: () => void;
};

Running the following command expands the type expression SomeType["e"]:

expand-my-type example.ts 'SomeType["e"]'
# Output:
# { a: number; b?: string; c: number }

API

This package exports a function named expandMyType that can be used in one of the following ways:

  1. Expand the type expression in a source file:

    Given a file named example.ts with the following contents:

    interface SomeInterface {
      a: number;
      b?: string;
      c: number | undefined;
    }
    
    type SomeType = {
      d: number;
      e: SomeInterface;
      f: () => void;
    };

    Running the following code expands the type expression SomeType:

    import { expandMyType } from "expand-my-type";
    
    const expandedType = await expandMyType({
      typeExpression: "SomeType",
      sourceFileName: "./example.ts",
    });
    
    console.log(expandedType);
    /* {
      d: number
      e: { a: number; b?: string; c: number }
      f: () => void
    } */
  2. Expand the type expression in a source text:

    import { expandMyType } from "expand-my-type";
    
    const expandedType = await expandMyType({
      typeExpression: "A<number>",
      sourceText: `
         type A<T> = {
           a: string;
         } & B<T>;
        
         type B<T> = {
           b: T;
         };
       `,
    });
    
    console.log(expandedType);
    /* { a: string; b: number } */

How It Works

Expand My Type uses the following utility types to expand the type expression:

// Expands a type expression
type Expand<T> = T extends (...args: infer A) => infer R
  ? (...args: Expand<A>) => Expand<R>
  : T extends Promise<infer U>
    ? Promise<ExpandTypeArgument<U>>
    : {
        [K in keyof T]: T[K] extends string ? ExpandString<T[K]> : Expand<T[K]>;
      } & {};

type ExpandTypeArgument<T> = [T & {}] extends [never]
  ? T
  : T & {} extends void
    ? T
    : Expand<T & {}>;

// Forces a union of string literal types to be expanded
type ExpandString<T extends string> = RemoveUnderscore<AppendUnderscore<T>>;
type AppendUnderscore<T extends string> = `${T}_` extends string
  ? `${T}_`
  : never;
type RemoveUnderscore<T extends string> = T extends `${infer U}_` ? U : never;

This approach comes with some limitations. Most notably, expanding a type that leads to an infinite recursion might throw an error (when prettify option is enabled), return a truncated output, or return any.