My goal is to make a set-method with prop_key and prop_value arguments that decide a type of prop_value on the value of a prop_key.
I want it to use enum as prop names and an interface as types of prop values.
What I want to do:
enum Property {
A = 0,
B,
C
}
interface PropertyStorage {
[Properties.A]: number;
[Properties.B]: string;
[Properties.C]: boolean;
}
setProperty(name: Property, value: TypeOfThisPropertyInStorage?): void
// Example
// setProterty(Propert.A, value => number)
// setProterty(Property.B, value => string)
// setProterty(Property.C, value => boolean)
Recently I've tried:
setProperty(name: Property, value: Pick<PropertyStorage , typeof name>): void
But when I try to use it I get:
setProperty(Property.A, 15)
// Argument of type 'number' is not assignable to parameter of type 'Pick<PropertyStorage , Property>'.
As @captain-yossarian mentioned- solution is
enum Property {
A = 0,
B,
C
}
interface PropertyStorage {
[Property.A]: number;
[Property.B]: string;
[Property.C]: boolean;
}
const setProperty = <Name extends keyof PropertyStorage>(name: Name, value: PropertyStorage[Name]) => {
}
setProperty(Property.A, 42)