microsoft / microsoft/TypeScript

Type instantiation for type level generic function

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

还没有人认领这个 Issue。

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

描述

### 🔍 Search Terms

type instantiation
type level generic function
generic return type

Related: https://github.com/microsoft/TypeScript/issues/40542, https://github.com/microsoft/TypeScript/issues/61133 and maybe https://github.com/microsoft/TypeScript/issues/52035?

Edit: Just found https://github.com/microsoft/TypeScript/issues/55435 and https://github.com/microsoft/TypeScript/issues/40179

### ✅ 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

#47607 added support for instantiation expressions, but those do not apply at the type level. I understand it'd be ambiguous in some cases (https://github.com/microsoft/TypeScript/pull/47607#issuecomment-1058192563), but in some cases there really isn't a reasonable workaround.

### 📃 Motivating Example

I actually have a very similar case to the example provided in https://github.com/microsoft/TypeScript/issues/40542

```ts
const provideBox = () => {
const box = (value: T) => ({value});
return box;
};

type BoxMaker = ReturnType
// type BoxMaker = (value: T) => { value: T; }

type Box1 = ReturnType>;
// Type 'BoxMaker' is not generic.(2315)

const box = undefined as unknown as BoxMaker;

type Box2 = ReturnType>;
//type Box2 = { value: T; }

```

Compiler Options

```json
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"target": "ES2017",
"module": "ESNext",
"moduleResolution": "node"
}
}
```

**Playground Link:** [Provided](https://www.typescriptlang.org/play/?jsx=0#code/MYewdgzgLgBADgJxANwJYBMCmAhEAPGAXhgAoBKIgPhgG8AoGRmUSWAI3yJgB4AVSksgCGAGwCumAFwxeFQtRI1h4zAF8yAbgZMEmKGIRgYHPFtVa6UAJ5xMMXHgCyQgNaYEXAEp6DYXjcxua1sQADN4JDQsB0o6AHo4mGC7B2c3D2I+AWUJaVkqWhgcqRkNGFU6SwD7fABGLK8fQ39bblTXdyzKLQSZaoBydvT+mFQIGDAQWABzTDB3VGAAOhIAJgBmWoBWMkqWaGNOYjEwLFDUefQYIXGTl0mAdyMbmqcOhAtk19WG4m99ZoBIIBMKHPBdHpxL4OH78Lg0IqiXKlcqVIA)

### 💻 Use Cases

1. What do you want to use this for?

In our case we have a more complex `provideBox` that accept arguments. We also need to rely on type inference for the generic type of the `box` function and its return value;

3. What shortcomings exist with current approaches?

I cannot extract and export a generic type for a return value of a generic function if I don't have direct reference to the function.

5. What workarounds are you using in the meantime?

The only workaround I found is to use a dummy runtime value to apply the type, and use and instantiation expression on that dummy value (see `Box2` above)

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先复现所提供 Playground 链接中的动机示例,然后阅读相关 issue 和 PR #47607,以了解现有的实例化表达式行为以及指出的歧义。完成的标准是:类型级形式可以在不使用虚拟运行时值的情况下提取泛型返回类型,生成预期的 Box2 结果,同时保持现有行为不变。

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

评估

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

把新 issue 发到你的邮箱

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