Microsoft/TypeScript
GitHub ã§èŠãError message displays computed name '[attr]' instead of key value
Open
#57,653 opened on 2024幎3æ5æ¥
Experience EnhancementHelp WantedSuggestion
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
ð Search Terms
computed property, error message
ð Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about computed properties
⯠Playground Link
ð» Code
type Unit =
| {
name: string;
stength: number;
}
| {
name: string;
magic: number;
};
const createUnit = (
name: string,
attr: "strength" | "magic",
power: number,
): Unit => {
return attr === "strength"
? { name, [attr]: power } // <-- error here
: { name, [attr]: power };
};
ð Actual behavior
Error message displays "Object literal may only specify known properties, and '[attr]' does not exist in type 'Unit'".
ð Expected behavior
Error message should display that "strength" doesn't exist on Unit. (and possibly even recommend "stength" instead)
Additional information about the issue
No response