I'd cast the return value of getMessageIdFromSomeObscureLogic() to a string literal like so: type messageTypes = "success" | "error" | "warning" | "bananas" | etc....
And I'd type the messages object like so: const messages: [key in messageTypes]: string;
Yes! that would work as well. And it results in a better error report:
The error now is on the messages object declaration (where the banana property is missing) as opposed to where we try to access it by key messages[id] ✅
The error message now reads "Property 'bananas' is missing..." even with TypeScript 2.4. ✅
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I'd cast the return value of
getMessageIdFromSomeObscureLogic()
to a string literal like so:type messageTypes = "success" | "error" | "warning" | "bananas" | etc...
.And I'd type the messages object like so:
const messages: [key in messageTypes]: string;
Yes! that would work as well. And it results in a better error report:
messages
object declaration (where thebanana
property is missing) as opposed to where we try to access it by keymessages[id]
✅"Property 'bananas' is missing..."
even with TypeScript 2.4. ✅