Let's say there is an object that looks like this:
const object = {
prop: "prop",
typedProp: "type-1",
};
I want the typedProp property to only accept the following values: "type-1", "type-2", "type-3". To do this, I created a custom type that looks like this:
type CustomProp = "type-1" | "type-2" | "type-3";
I know that I can assign this type like so:
const object: {
prop: string;
typedProp: CustomProp; // <-- Adding type here
} = {
prop: "prop",
typedProp: "type-1",
};
My question is, is there a way to assign this type directly to typedProp (within the object) so that I can avoid adding the type to the whole object? I tried to implement it this way:
const object = {
prop: "prop",
typedProp: "type-1" as CustomProp,
};
but it doesn't work as expected, because with this approach, I can add any string to typedProp, which is not what I would like to achieve. Could you please advise if there is a way to do this?