I'm trying to type properties of the React component in a way that when one of the properties has specific value then the type of the other property changes. Overall, I know how to do it if the input type is something like string | number but I don't know how to do it if it's string | "specificString". Maybe it's not even possible?
Here are my two attempts to achieve that.
type Action = "createItem" | "updateItem" | "deleteItem";
type Props<TName = string> = {
name: TName;
value: TName extends "_action" ? Action : string;
};
const props: Props = {
name: "_action",
value: "wrongAction", // I want it to error
};
Second try:
type Action = "createItem" | "updateItem" | "deleteItem";
type Props = {
name: "_action";
value: Action;
} | {
name: string;
value: string;
};
const props: Props = {
name: "_action",
value: "wrongAction", // I want it to error
};
Any ideas how to achieve that? Thanks!