microsoft / microsoft/TypeScript

Feature Request: Instantiate Generic Functions within Conditional Types

未关闭
#61,133 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Awaiting More Feedback Suggestion
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

🔍 Search Terms

Instantiation Expressions, Conditional Types

✅ Viability Checklist
⭐ 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.

Parameterized Type

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
  1. What do you want to use this for?

Implement generic types in a type level syntax parser.

  1. What shortcomings exist with current approaches?

Cannot instantiate generic signatures within a conditional type.

  1. What workarounds are you using in the meantime?

I'm not sure there are workarounds.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先从示例中描述的条件类型和实例化表达式行为开始,然后查看相关 issue #22617。完成的标准是:泛型函数签名可以使用条件类型提供的类型参数进行实例化,包括所示的 Infer 和 Vector 情况,同时不改变生成的 JavaScript。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。