microsoft / microsoft/TypeScript
Feature Request: Instantiate Generic Functions within Conditional Types
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
### 🔍 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.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、例で説明されている conditional type と instantiation expression の動作から始め、関連する issue #22617 を確認します。conditional type から提供された型引数を使ってジェネリック関数のシグネチャをインスタンス化でき、示されている Infer と Vector のケースも含めて、出力される JavaScript を変更しないことが完了の条件です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100