microsoft / microsoft/TypeScript
type callbacks
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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