My mission is to create a type safe mapping object where I can define key/value pairs just once.
I've managed to achieve this with the code below:
const myPropTuple = [
[0, "cat"],
[1, "dog"],
[2, "bird"]
] as const;
type TMyPropKey = TInnerTupple<typeof myPropTuple>[0];
type TMyPropValue = TInnerTupple<typeof myPropTuple>[1];
function getMyProp(val: number) {
type TKey = TInnerTupple<typeof myPropTuple>[0];
const mapA2 = new Map(myPropTuple);
if(!mapA2.has(val as TKey)) throw new RangeError("unexpected value");
return mapA2.get(val as TKey);
}
// helper library (not to be inlined)
type TTupleType<T extends Iterable<any>> = T extends ReadonlyArray<infer R> ? R :never;
type TInnerTupple<I extends Iterable<any>> = TTupleType<I>;
// Tests
console.assert(getMyProp(1) === "dog");
//console.assert(getMyProp(1) === "turle"); // throws compiler error
const a: TMyPropValue = "cat";
//const b: TMyPropValue = "eagle"; // throws compiler error
Now, I want to make the function generic while still maintaining type safety:
The objective is to write
const myPropTuple = [
[0, "cat"],
[1, "dog"],
[2, "bird"]
] as const;
console.assert(getGenericProp(myPropTuple, 1) === "dog");
const yourPropTuple = [
[0, "fish"],
[1, "towel"],
[2, "whale"]
] as const;
console.assert(getGenericProp(yourPropTuple, 0) === "fish");
and ensure that the following examples fail to compile
console.assert(getGenericProp(myPropTuple, 1) === "turle"); // throws compiler error
type TYourPropValue = TInnerTupple<typeof yourPropTuple>[1];
const y: TYourPropValue = "dog"; // throws compiler error
addendum
@jcalz proposed an alternative solution which focuses on simplicity, so here is a slightly expanded version:
const animalProps = {
0: "cat",
1: "dog",
2: "bird"
} as const;
function getGenericProp2<T extends object>(props: T, val: keyof T): T[keyof T] {
const found = props[val];
if(found === undefined) throw new RangeError("unexpected value");
return found;
}
type TValues = ValueOf<typeof animalProps>;
type TKeys = keyof typeof animalProps;