microsoft / microsoft/TypeScript
Feature Request: Instantiate Generic Functions within Conditional Types
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
### 🔍 Search Terms
Instantiation Expressions, Conditional Types
### ✅ Viability Checklist
- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] 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.
```typescript
// some method
declare function method(value: T): T
// ... this is possible
type R0 = ReturnType>
// ... but this isn't
type Infer(value: T) => unknown, U> = ReturnType>
type R1 = Infer
```
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.
[Parameterized Type](https://www.typescriptlang.org/play/?moduleResolution=99&module=199#code/JYWwDg9gTgLgBAbzgZQJ4DsYEMAecC+cAZlBCHAOQACAzsOgMYA2WwUA9DKmAKYBGEHOxoZsOCgChQkWIhTYYwBgWKly1OoxZtO3foMkSGEdDXgAVOAF4Uo3AAoK6AK4g+PKBQCUcCUZNmcABqPAww0Na2mA5IlvgANHAABkg4AFxw5omoGVlwAF65BElefly8waHhUJHICkoAPOU8EESVYdAAfBLs7HD9AHoA-H5AA)
```typescript
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)
```typescript
const Vector = (T: T) => Type.Object({ x: T, y: T, z: T })
const T = Syntax({ Vector }, `Vector`) // 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](https://www.typescriptlang.org/play/?moduleResolution=99&module=199&ssl=9&ssc=91&pln=3&pc=1#code/JYWwDg9gTgLgBAbzgFQMoGMAWBTEBDAGhQE8xsjkA5AVxACNso4BfOAMyghDgHIABAM7AAdugA2eYFAD0MUtjoQAHjwBQq9BGED4ANWzoY0OAF44AHmRxsSmNmEATASgw58APgAUyAFwoAlKbuJGQAdADydABWBjCeSEp+yETESUQAXkks-urS0nChhXAidlDCeGJwRlU4cGB4UAKM6nJkcAAKDU0ONPSMpii9DEwt8nAAktoweKLYDgMAStgw1GXI8uat2BBscPqG0OadjXNDjO7BeXDC2HM1wM4AbsB4cJqOwDDAWhVV8qpAA))
```typescript
const Vector = (T: T) => Type.Object({ x: T, y: T, z: T })
// ... internal to the parser
type ParsedNumber = TNumber
type Instanced = ReturnType> // need this via conditional type
```
### 💻 Use Cases
1. What do you want to use this for?
Implement generic types in a type level syntax parser.
2. What shortcomings exist with current approaches?
Cannot instantiate generic signatures within a conditional type.
3. What workarounds are you using in the meantime?
I'm not sure there are workarounds.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con il comportamento dei tipi condizionali e delle espressioni di istanziazione descritto negli esempi, quindi esamina l’issue correlata #22617. Il lavoro è completato quando le firme delle funzioni generiche possono essere istanziate con argomenti di tipo forniti da un tipo condizionale, inclusi i casi mostrati di Infer e Vector, senza modificare il JavaScript emesso.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100