Skip to content

[performance] lazy types #40866

@samdenty

Description

@samdenty

Search Terms

type performance. computed types. lazy types

Suggestion

The ability to mark a type as lazily computed.

Lazily computed means the type is fully ignored if it's not referenced from anywhere else in the code base.

Use Cases

For gqless we have a complex TypeData<schema.TypeName> type, which takes a GraphQL schema type and transforms it into a data type.

Doing this is relatively cheap for a single type. But for some APIs (GitHub for example) there can be thousands of different types. Including 1000 unreferenced calls to the TypeData type vastly slows down the entire editor, even when editing non-related source files.

These types are guaranteed to be correct, so there's no need to run the type checker on them if they're not referenced anywhere.

Without the ability to mark types as 'lazy', the only other options are to either create thousands of files with the types in them - or resort to some background process which manually comments out types when not in use.

Examples

// @ts-lazy
type MyType = ComputeIntensiveType<1, 2>

// if MyType resulted in a type error, the build would still pass
// because MyType wasn't actually used anywhere

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions