microsoft / microsoft/TypeScript
Feature Request: Instantiate Generic Functions within Conditional Types
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
🔍 Search Terms
Instantiation Expressions, Conditional Types
✅ Viability Checklist
- 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, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
I am looking for a way to be able to instantiate a generic function / return type via conditional type.
// some method
declare function method<T>(value: T): T
// ... this is possible
type R0 = ReturnType<typeof method<number>>
// ... but this isn't
type Infer<Func extends <T>(value: T) => unknown, U> = ReturnType<Func<U>>
type R1 = Infer<typeof method, number>
Related Issue: https://github.com/microsoft/TypeScript/issues/22617
📃 Motivating Example
I have written a small type-level parser for one of the libraries I manage. I have support for type parameterization.
const T = Syntax('number')
const Vector = Syntax({ T }, `{ x: T, y: T, z: T }`) // const T: TObject<{
// x: TNumber,
// y: TNumber,
// z: TNumber
// }>
... but would also like to support generic functions as parameters (and resolve exterior types via function call / instantiation expression)
const Vector = <T extends TSchema>(T: T) => Type.Object({ x: T, y: T, z: T })
const T = Syntax({ Vector }, `Vector<number>`) // const T: TObject<{
// x: TNumber,
// y: TNumber,
// z: TNumber
// }>
When implementing the second case, I noticed it's not actually possible to instantiate the Vector signature within the context of a conditional type expression (i.e. the parser). In this case, I have a parsed TNumber type ready to supply the generic Vector function, but no way to instantiate it.
Essentially I would need the following but in a conditional type (Reference)
const Vector = <T extends TSchema>(T: T) => Type.Object({ x: T, y: T, z: T })
// ... internal to the parser
type ParsedNumber = TNumber
type Instanced = ReturnType<typeof Vector<ParsedNumber>> // need this via conditional type
💻 Use Cases
- What do you want to use this for?
Implement generic types in a type level syntax parser.
- What shortcomings exist with current approaches?
Cannot instantiate generic signatures within a conditional type.
- What workarounds are you using in the meantime?
I'm not sure there are workarounds.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par le comportement des types conditionnels et des expressions d’instanciation décrit dans les exemples, puis examinez l’issue associée #22617. C’est terminé lorsque les signatures de fonctions génériques peuvent être instanciées avec des arguments de type fournis par un type conditionnel, y compris les cas Infer et Vector présentés, sans modifier le JavaScript généré.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100