I came across a solution called KeysOfType on a post at :
type KeysOfType<T, TProp> = { [P in keyof T]: T[P] extends TProp? P : never }[keyof T];
Here are the interfaces being used:
interface SomeInterface {
a: number;
b: string;
}
interface AnotherInterface {
a: number;
b: string;
c: number;
d: string;
}
interface DataInterface {
someProp: SomeInterface;
anotherProp: AnotherInterface;
...
}
When implementing it as shown below, an error occurs:
Type 'SomeInterface' is not assignable to type 'SomeInterface & AnotherInterface '.
Type 'SomeInterface ' is missing the following properties from type 'AnotherInterface ': c, d
const setProperty = (numberValue: number, stringValue: string, propName: KeysOfType<DataInterface, SomeInterface>) => {
const obj: SomeInterface = {
a: numberValue,
b: stringValue
};
data[propName] = obj; // data is DataInterface
}
Is there a way to retrieve property keys of DataInterface that precisely match the type of SomeInterface?