I have the follolwing working code:
interface Box {
length: number;
width: number;
height: number;
}
const box: Box = {
length: 2,
width: 1,
height: 3
};
type PickWithArray<T, A extends readonly (keyof T)[]> = Pick<T, A[number]>;
const keysArray = ["length", "width"] as const;
type Extracted = PickWithArray<Box, typeof keys2>;
This works great, but when I create keysArray
, I'd like to constrain the array's values to keys of the type so I can get autocomplete in my IDE. I've attempted the following:
type PickWithArrayKeys<T> = Array<keyof T>;
const pickedKeys: PickWithArrayKeys<Box> = ["length", "width"];
const keysArray = [...pickedKeys] as const; // Doesn't work, original type is persisted
type Extracted = PickWithArray<Box, typeof keysArray>;
This doesn't work, as typeof keysArray
is actually ("length" | "width" | "height")[]
so Extracted
ends up being:
type Extracted = {
length: number;
width: number;
height: number; <-- want this to be excluded
}