🔎 搜索词
is:issue runtime caching as const
🕗 版本与回归信息
- 这是类型检查器运行时不一致的情况
- 自引入const泛型以来,这是我在每个版本中尝试的行为
⏯ Playground链接
https://www.typescriptlang.org/play/?target=99#code/C4TwDgpgBA8gRgKwgY2AMQE4HsC2BRAO2AwEsIBnAHgBUA+KAXimqggA9gICATcqDCAENuWAgBsQUANokCAMwgYoAaQiT2nHn3LFZAcygAfKAQCuOOIqNRyIC1jEAaKLIVKAaoLGmIAXSm+AFBQUAD8UADe0gDWai4EKmq+AFxQnt7QAL7BUKkEEABuigDcgYGgkIkg6T4ACoIkGFR0jJE5UsrxULEgWHLMKdLKztQdvr6lmVI9fQOlgexgWBjAUHKmBKgkolA4gmDwSKiEuhSUOciiOsysHFy8UFiIKMCOF1er1ACy+5Dct5oHlIdKQCAZjGYLFZjLZ7E4oBtogQsAB3Ai+N60AAUOSeCFS1DeIT2YEgGFSWKkgmccEGqmqXjqDSaNFoAEpGPRvr8INw3mzUocXphcCdSGduaTeQF6BEcgB6eUAAWA5AAtIsXhqMNglABBMQCYSSMDYMkSKAVXmWgAW2FMehttug5BIegIgmApgEOQEXowCSFqAAdHJsPgiOLyFig8Bg1xTtG8WzgySsSSyWy2ZMypcCNdONcmCTY2KyNGooJUgBGGmpABMUEyzkpbGcIF8HIY9CkbagHezZSAA
💻 代码
type ObjectFromEntries<T> = T extends readonly [infer Key extends string | number | symbol, infer Value][]
? { [key in Key]: Value }
: never;
type KeyValuePairs<T> = {
[K in keyof T]: [K, T[K]];
}[keyof T];
export function mapObjectEntries<
const T extends object,
const TMapped extends [string | number | symbol, unknown],
>(
obj: T,
mapper: ([a, b]: KeyValuePairs<T>) => TMapped,
): ObjectFromEntries<TMapped[]> {
//@ts-expect-error Already properly typed through the signature
return Object.fromEntries(Object.entries(obj).map(mapper));
}
const test = mapObjectEntries({ a: 1, b: 2 }, ([x, y]) => [x, y]);
这里有一个屏幕录制,说明了这个问题:
vivaldi_gWZYW9aEDa.mp4
🙁 实际行为
类型检查器是上下文敏感的。当我在表达式后面添加一个 as const
(参见屏幕录制),即使我再次删除 as const
,它也能正常编译。
🙂 预期行为
一致性
关于此问题的其他信息
- 无响应*
1条答案
按热度按时间xlpyo6sf1#
基于报告的问题准备的失败的测试用例: