Struggling to make TypeScript infer the second argument based on the type of property "data" in the first argument? Looking for tips on setting up type DialogHavingData
.
type DialogHavingData<T> = /* Need help with this part */
'data' in T ? T['data'] : any;
class MyDialog {
data: {
prop1: string;
prop2?: string;
prop3: {
name: string;
age?: number;
}
};
}
class MyDialog2 {
// Data is optional
}
function createDialogFromClass<T = any, Y extends DialogHavingData<T>>(dClass: T, payload: Y) {
// Implementation goes here...
}
// When MyDialog has a "data" property, the second argument should match its type
createDialogFromClass(MyDialog, {
prop1: "my string",
prop3: {
name: "Test Name",
age: 12
}
// It should prompt for prop1 and prop2 types
})
// Any value allowed as MyDialog2 doesn't specify the data property
createDialogFromClass(MyDialog2, null)
createDialogFromClass(MyDialog2, {test: "My test dialog"});