Consider the following scenario with an interface:
interface ITest {
a: number;
b: string;
}
In this case, the goal is to create an implementation where a function only requires an object that contains a specific subset of properties from the defined interface. For example, it may only need the a
property. Initial attempts at achieving this yielded the following code snippet:
type WithOnly<T, K extends keyof T> = {
[K in keyof T]: T[K];
}
export const f = (x: WithOnly<ITest, 'a'>) => settings.a * 2;
However, upon testing, the compiler flagged an issue where it expected the input parameter x
to also contain the b
property. Is there a workaround to successfully implement this requirement?