Skip to content

Cache specific utility types #41263

@tjjfvi

Description

@tjjfvi

Search Terms

cache types

Suggestion

(WIP; syntax bikeshed-able)

A new keyword expensive, used as below

expensive type T<U> = /* ... */;

When a generic type uses the expensive keyword, it marks it to be cached by the compiler.
The compiler will hash the type alias (generics unresolved) and the passed type parameters, and use that to memoize the resulting type.

Suggestions for hashing the types:

  • For referenced external type aliases, hash the definition recursively
  • For unique symbol types and classes with private properties, it also hashes the relevant name & source file path
  • For type parameters & infers, it labels them T0, T1, etc. (as they appear), and hashes them as such
  • Object types & interface types hash their properties in a specific order
  • Labeled tuples do not hash their labels
  • JSDoc is ignored (possible exception of Feature Request: Documented Utility Type #41165)

Use Cases

With complex utility types, perf can often be sluggish as "semantic information produced from type computation is recomputed fresh on each edit"

Suggestion adapted from a comment on #41254

Examples

expensive type SplitStr<T extends string> = T extends `${A}${B}` ? [A, ...SplitStr<B>] : B;

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions