microsoft / microsoft/TypeScript

Instantiable types from return types of contextual signatures don't provide contextual type information for return expressions

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

还没有人认领这个 Issue。

Domain: check: Contextual Types Help Wanted Possible Improvement
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

### 🔎 Search Terms

instantiable contextual signature return types

### 🕗 Version & Regression Information

- This is the behavior in every version I tried

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.0-dev.20250216#code/C4TwDgpgBAwg9gO2BAHsAysAhsgzlAXgCgooAfKAbxNNt22AFdcAuKAIgBs4sATASwQBzdgG4atKLxxY2CRp07jJAXxoVqk0vRzM27XIwDGRiLlxiJtadjb0AToKHLaK8UV4QjnLPegAzRgQjYH5EKCM-HAhMOD8AHgAVeCRUYAA+AAoaI0RkNDZkvLSAGhzEf34hNk1aRDYAJS843niHJxKoTJCUQpT84ABKQnSoItS0dJcVMsG2ADc4fl53XIR6KHo46AIIqORYv2zSWu0GPQ5uPid2MskbWSh5RTuoFSgsfH60zGjcV9OUHqVCspH8EGARgAFmxMsMCKNMoDJDomKwOIYTGYLK8tFIZPooRBFHBbqC3oNcW9XjMiINxEA

### 💻 Code

```ts
type ContextStates =
| {
status: "loading";
data: null;
}
| {
status: "success";
data: string;
};

declare function createStore(
context: TContext,
config: {
on: Record TContext>;
},
): void;

const store = createStore(
{
status: "loading",
data: null,
} as ContextStates,
{
on: {
fetch: () => ({
status: "success",
data: "hello",
}),
},
},
);
```

### 🙁 Actual behavior

`status: "success",` errors

### 🙂 Expected behavior

no error

### Additional information about the issue

Making `(ctx: TContext) => TContext` into a type parameter makes it work (see [TS playground](https://www.typescriptlang.org/play/?ts=5.8.0-dev.20250216#code/C4TwDgpgBAwg9gO2BAHsAysAhsgzlAXgCgooAfKAbxNNt22AFdcAuKAIgBs4sATASwQBzdgG4atKLxxY2CRp07jJAXxoVqk0vRzM27XIwDGRiLlxiJtadjb0AToKHLaK8UV4QjnLPegAzRgQjYH5EKCM-HAhMOD8AHhoAFXgkVGAAGmSk+ywEXH5Q8PSIBF58AAoQlDYUxGQ0AEpCAD4oOrS0LJaKmiN69NrUhsy+xH9+ITZNWkQ2ACUvON54hycM9py8gqKEFpcVLMa2ADc4fl53fvzgKHo46AIIqORYv17SGe0GPQ5uPic7Cykhssig8kUwKgKigWHww3SmGiuChXygcyoVlI-ggwCMAAs2BVmgQ2hU0ZIdExWBxDCYzBYoVopDJ9PiIIo4ECsdDGkzoVDDkRGuIgA)) but that's not natural to write and isn't broadly applicable as a workaround

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先运行链接的 TypeScript Playground 复现,并将其与提供的泛型类型变通方案进行比较。调查为什么上下文签名中的返回表达式会被拒绝,并在原始示例无需该变通方案即可通过类型检查时,视为 issue 完成。

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

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

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