microsoft / microsoft/TypeScript

type callbacks

Open
#29,755 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Proposal Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

search: https://www.google.com/search?q=type+functions+site%3Ahttps%3A%2F%2Fgithub.com%2FMicrosoft%2FTypeScript

The more I work with advance types the more apparent the following problems become:

No intermediate types

At the current stage the advanced types are represented via expressions. Quite often there is a need to reuse exactly the same part of such expression in more than one place. Currently there is no way to capture a part of it and reuse it, consider:

type A<X> = { x: B<X>, y: B<X> }

here I wish I could save B<X> into a local type C somehow to be able to write

// speculative syntax:
type A<X> = <
    type C = B<X>;
    { x: C, y: C };
>;

per @RyanCavanaugh intermediate types are covered by https://github.com/Microsoft/TypeScript/issues/23188

No way to specify a type callback

Currently type parameters are the only way to define a parametric type. There is no way to define a type based on a type callback:

interface Z { x: string; y: number; }
// speculative syntax:
type A<T, F: X =>...,  T> =  { [P in keyof T]: F<T[P]>; } // <-- wish could do this
type B = A<Z, X => X extends string ? true : false>;  // { x: true; y: false; }
type C = A<Z, X => { get: () => X }>;  // { x: { get: () => string; }; y: { get: () => number; }; }

I am not sure what a proposal would be, but these problems are definitely worth a discussion

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the proposed type-callback examples in this issue and the linked issue #23188, which covers intermediate types. The issue does not name implementation files or tests, and it requests discussion rather than a defined change. A completed effort would need an agreed proposal for expressing and applying type callbacks.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.