I need to force an array to have a specific set of values that should be then the keys of my interface. I can force the array with
type SomeProperties = ['prop1', 'prop2', 'prop3'];
but I don't know how to force an interface to have those properties. I tried something like
type MyInterface = {
[key in keyof SomeProperties]: string;
}
but obviously the keys of an array are just numbers so my interface become
interface MyInterface {
0: string;
1: string;
2: string;
}
instead of the wanted interface
interface MyInterface {
prop1: string;
prop2: string;
prop3: string;
}
Do you know if there is a way to achieve this in Typescript?
It would be useful because I need to iterate on some properties to "clone" an object and I also need to access to those properties easily. Repeating the properties in both type and interface is a bit fragile for me.
keyof SomePropertiestoSomeProperties[number].length,at,charAtand so on but notprop1,prop2orprop3:(keyof SomeProperties[number]; justSomeProperties[number].