score:1

Accepted answer

unless some very clever person has a better solution, this should work:

const intent = {
    none: "none" as "none",
    primary: "primary" as "primary",
    success: "success" as "success",
    warning: "warning" as "warning",
    danger: "danger" as "danger",
};
type intent = typeof intent[keyof typeof intent];

const classes = {
    intent_primary: "foobar",
    intent_none: "barfoo",
    intent_success: "somecssclass",
    intent_warning: "you get the idea",
    intent_danger: "will robinson",
};

type intentkey = keyof typeof intent;

const keymap: {
    [key in intentkey]: keyof typeof classes
} = {
    primary: "intent_primary",
    none: "intent_none",
    success: "intent_success",
    warning: "intent_warning",
    danger: "intent_danger",
}

function test({ color }: { color: intentkey }) {
    return classes[keymap[color]];
}

notice that this is type-safety at the cost of an explicit mapping.

playground


Related Query

More Query from same tag