microsoft / microsoft/TypeScript

Inconsistent enum literal types

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

还没有人认领这个 Issue。

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

描述

### 🔎 Search Terms

return type inference enum union literal fresh regular

### 🕗 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/KYOwrgtgBAolDeUCCAaKAhKBfAUDgLgJ4AOwGA9gB4A8AKgHxQC8COUUA5sPgFxQAUASmaNaAbjZQAztz78AbgEMANmGB9awpo3nkAlgBMJWCTgPAAxssUAnMgDMwIC-j3kQUAEZU69BSrUNQT50HwZTC3cpfC8QAEZmLyp+AAZBMSgAekyKGnAIT2AbehxIkGjYgCYQn3zC4sTvSlT0rJyAeQBrPDKKwoSWJv4YADokVuzc6hgS3pjC6qmZxuTR8YzJmBsbchs0JumxxhByGMUpKT0OEEVPZTJ8ciWSoA

### 💻 Code

```ts
enum E { A, B }

type Box = {
get: () => T;
set: (value: T) => void;
};

declare function box(value: T): Box;

const bn1 = box(0); // Box
const bn2: Box = box(0); // Ok

const be1 = box(E.A); // Box
const be2: Box = box(E.A); // Error, box not assignable to Box
```

### 🙁 Actual behavior

`be2` errors

### 🙂 Expected behavior

`be2` shouldn't error

### Additional information about the issue

This is an enum-based variant of https://github.com/microsoft/TypeScript/issues/48363 . As such it relates to the open https://github.com/microsoft/TypeScript/issues/59754 too

贡献指南

打开贡献指南

从这里开始

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

调研方向

从链接的 TypeScript Playground 复现开始,比较 bn1、bn2、be1 和 be2 的推断类型。阅读相关 issue #48363 和 #59754,了解现有上下文;当基于 enum 的 be2 赋值不再报错,同时保留预期的推断行为时,即表示完成。

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

评估

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

把新 issue 发到你的邮箱

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