Microsoft/TypeScript
在 GitHub 查看Unhelpful TS2322 error message when assigning a type to another type.
Open
#50,114 建立於 2022年7月31日
BugDomain: Error MessagesHelp Wanted
倉庫指標
- Star
- (48,455 star)
- PR 合併指標
- (平均合併 6天 17小時) (30 天內合併 9 個 PR)
描述
Bug Report
🔎 Search Terms
TS2322 unhelpful message
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about
TS2322
⏯ Playground Link
Playground link with relevant code
💻 Code
type StringMap = {
[x: string]: string;
}
type Message<TMap extends StringMap, T extends keyof TMap> = {
bar: "bar";
type: T;
}
type MessageHelper<TMap extends StringMap, T extends keyof TMap> =
T extends keyof TMap ?
Message<TMap, T> :
never;
function foo<TMap extends StringMap, T extends keyof TMap>(type: T) {
const message : Message<TMap, T> = {
bar: "bar",
type,
}
const message2 : MessageHelper<TMap, T> = message;
// ^^^^^^^^------- Type 'Message<TMap, T>' is not assignable to type 'MessageHelper<TMap, T>'.
message.type; // T extends keyof TMap
message2.type; // (keyof TMap & string) | (keyof TMap & number) | (keyof TMap & symbol)
message.type = message2.type;
// ^^^^^^^^^ This is more helpful, but I had to jump through quite some hoops to finally figure
// out this was the cause.
}
🙁 Actual behavior
The error that occurs when assigning message to message2 shows just
Type 'Message<TMap, T>' is not assignable to type 'MessageHelper<TMap, T>'.
🙂 Expected behavior
A better explanation of why the types are not assignable. I'm assuming it's because the types of message.type are different. But this isn't clearly shown in the error message.