I am currently in the process of converting a JavaScript function to TypeScript. Originally, I believed that the type of the variable hi
would be ('s'|'bb')[]
, but it turned out to be string[]
. Is there a way for TypeScript to automatically infer K to be the keys of NewOptions?
export const getWithOverrides = <
K extends string,
NewOptions extends { [x in K]?: boolean },
Overrides extends { [x in K]?: boolean }
>(
newOptions: NewOptions,
overrides: Overrides
): K[] =>
(Object.keys(newOptions) as K[]).filter(key => {
if (typeof overrides[key] === 'boolean') {
return overrides[key];
}
return newOptions[key];
});
const hi = getWithOverrides({ s: true, bb: true }, { s: true });