🔎 搜索词
exactOptionalPropertyTypes optional generic indexed access undefined missing contextual constraint eopt
🕗 版本与回归信息
- 在5.3.0-dev.20230914中发生了变化
⏯ Playground链接
https://www.typescriptlang.org/play?exactOptionalPropertyTypes=true&ts=5.4.0-dev.20240111#code/PTAEAEFMA8EMGMAuB5ADoglgewHawDYAKATlqpMYgJ4AqV5AzgFyiLECukAUFxjohQBmCSKAAaAZUSwBAYVyCMAc3bEZ2HKADeXUKwAWkALaQA-CwBEsdoiwXQAH1AWAJrGIBre04v5l+xAsAbl1QADcMSAB3eXx2IxxzZwAjSAYMF0hvZwRMMKyQgF8eTPh8d1FBdhwkDVAlSEQANQZ4LEz5HEUVNUxcAB4aAD4ACgZIWtwWBjY+JQBKFhpHUGrMxRxIFxCuGFQsSlAqmr7NBsRO7tV1AYA5WBNQGAEcFwZQD0gqLEFxKRlIJdlNdTqNQngTCx7iYADRcRZ-aRyBTA3oaADa0MgAF1tKE2jgZvVIJtiBh4ABBBjpJR4ZIYPzUGhpRAsSRIwEono3HCYh440AAXlWr0gGy2QVAICexFIxB4emIjVUZ0aLTaHS5II0IwhkHmkulFDlXGKXAJRIJ8CVAipNLpDIwTJZbP+yK6qJ56M+31+7IBQO5p1xwrWYr4EqlYCwHi4QA
💻 代码
// @exactOptionalPropertyTypes: true
interface XStateConfiguration {
theme?: "auto" | "dark" | "light";
viewColumn?: "beside" | "active";
}
declare function getVscodeConfiguration<T>(section: string): T | undefined;
export function getConfiguration<Name extends keyof XStateConfiguration>(
name: Name,
): XStateConfiguration[Name] {
const genericAssignabilityTest: XStateConfiguration[Name] = undefined; // error
return getVscodeConfiguration(name); // error
}
const concreteAssignabilityTest: XStateConfiguration[keyof XStateConfiguration] = undefined; // ok
🙁 实际行为
undefined
不能像这样分配给表达式空间中的索引访问
🙂 预期行为
我希望这能通过类型检查 - 就像具体的示例一样
关于问题的附加信息
- 无响应*
2条答案
按热度按时间af7jpaap1#
嗯,这很棘手,因为在EOPT下,
XStateConfiguration[Name]
不一定具有不同的读写类型(只要它保持通用)吗?li9yvcax2#
是的,确实如此。我还没有花时间去考虑这个问题,但很可能是我的一个PR打破了这个限制:#55585