I need to figure out the best approach for implementing TypeScript types that only allow specific string values such as:
export type Plan = {
client_uuid: string;
name: string;
kind: string;
};
I want to create a subtype of Plan called Financial, which should only accept the strings "protection" or "insurance", and another subtype called Mortgage that accepts only "mortgage".
Is the following code the only way to achieve this?
export type Plan = {
client_uuid: string;
name: string;
};
export type Financial extends Plan {
kind: "protection" | "insurance"
}
export type MortgagePlan extends Plan {
kind: "mortgage"
}
It seems like this may not be the most ideal way to create subtypes since it involves re-declaring the kind property each time. Is there a way to specify that a Financial Plan is simply a type of Plan with a restricted kind field?
What other options are available in this situation?