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:
type CssLayout = {
display: "flex" | "grid" | "block";
}
type CssLayout = {
display: "flex" | "grid" | "block";
}
then inside code:
const someData = {
display: <const> "flex",
...
...
}
const someData = {
display: <const> "flex",
...
...
}
Is this idiomatic TypeScript? or am I commiting a crime?
2 Replies
Rhys
Rhys8mo ago
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
Boris Povolotsky
It is, but I figured using as Alignment makes more sense.