Microsoft/TypeScript
Auf GitHub ansehenError message displays computed name '[attr]' instead of key value
Open
#57.653 geöffnet am 5. März 2024
Experience EnhancementHelp WantedSuggestion
Repository-Metriken
- Stars
- (48.455 Stars)
- PR-Merge-Metriken
- (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)
Beschreibung
🔎 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