Is it idiomatic TypeScript: using as const?
Hi, this is typescript related.
Is there a way to avoid <const> or as const hell?
I have an object type that has several fields that are unions for example:
then inside code:
Is this idiomatic TypeScript? or am I commiting a crime?
2 Replies
Const isn’t even a valid operator there is it?
Either put a type on someData, I.e
someData: {display: “flex” | “block” } or cast display to the type, I.e
{
display: “flex” as Alignment
}
Typescript infers display as a string rather than your union type since someone could do someData.display = “foo” in between you declaring it and using it so you need to strongly type it
It is, but I figured using as Alignment makes more sense.