Looking to create a unique interface
interface CustomObject {
x: string;
y: number;
z: string[];
}
I am in need of a specialized function
function customFunction(obj: CustomObject, property: keyof CustomObject, data: any)
Currently, the 'data' parameter can be anything. However, my goal is to restrict it based on the property type.
- If
property
isx
, thendata
must be a string - If
property
isy
, thendata
must be a number - and so forth
I attempted using data: CustomObject[property]
, but encountered issues as it seems unable to reference another parameter. How do I accomplish this in TypeScript?