Skip to content

Declaration emit for named inferred types used in further inference inlines inferred types instead of naming them #2701

@chriskrycho

Description

@chriskrycho

Steps to reproduce

When emitting types for a type that TS must infer, e.g. the members of a union that derive from some other type, and that inferred type is used in another context where inference is involved (such as const foo = ... where the type of foo is not explicitly specified), tsgo will inline the type even if it is bound to a name.

This is enough to trigger the behavior:

const values = ["a", "b"] as const;
type Value = typeof values[number];

export const getValue = (): Value => "a";

This can cause significantly slower type checking for packages that consume these emitted declarations. For example, we have one project (using tsgo) that regressed from type checking in ~35s to taking over 6m 30s after a package that it depended on started using tsgo for emit.

This repo has several reproductions.

Behavior with typescript@6.0

TS emits named references to the inferred types when they have an intermediate name in cases like this:

export declare const getValue: () => Value;

Behavior with tsgo

tsgo emits an inlined version of the type alias.

export declare const getValue: () => "a" | "b";

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions