Microsoft/TypeScript

[exactOptionalPropertyTypes] Unexpected 'undefined' arises in error message

Open

#60,712 创建于 2024年12月7日

在 GitHub 查看
 (1 评论) (0 反应) (0 负责人)TypeScript (6,726 fork)batch import
BugDomain: flag: exactOptionalPropertyTypesHelp Wanted

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

🔎 Search Terms

"not assignable", "never", "undefined", "exactOptionalPropertyTypes"

🕗 Version & Regression Information

4.3.5 'undefined' error instead of 'never' ; the value undefined is considered valid 4.4.4, 5.0.4 Expected behavior 5.1.6, 5.7.2 'undefined' error instead of 'never' ; the value undefined triggers the 'never' message Nightly not available when testing

⏯ Playground Link

https://www.typescriptlang.org/play/?exactOptionalPropertyTypes=true&ts=5.7.2#code/C4TwDgpgBAKlC8UCwAoKUDeUCGAuKAdgK4C2ARhAE4A0UZA-PgRAG5VQC+UAPqultkaFWVWmSakKlTgG5UqAMYB7AgGdgUYPjiIMfHPgAM1feKiHUHGUA

💻 Code

--exactOptionalPropertyTypes

type T = 
  { a: number, b?: never } |
  { a?: never, b: number };

const t: T = { //  Types of property 'b' are incompatible. Type 'number' is not assignable to type 'undefined'
  a: 0,
  b: 0
};

🙁 Actual behavior

Type 'number' is not assignable to type 'undefined' is not relevant, since undefined cannot be the type of the t[b].

🙂 Expected behavior

The error should be about type 'number' not being assignable to type 'never'. It is correctly displayed if the value undefined is used (after the regression at least).

贡献者指南