My goal is to return a promise if the user selects true for async.
In TypeScript, it appears that the type will always be taken from the Create interface. How can I create a type that will be used to specify the return type?
interface Create<T> {
options: {
async: boolean;
};
properties: T;
}
type Return<T extends Create<any>> = T["options"]["async"] extends true ? Promise<boolean> : boolean;
const customCreateFunction = <T>(value: Create<T>): Return<Create<T>> => {
return true;
};
interface User {
username: string;
password: string;
}
// This does not return a promise
customCreateFunction<User>({
options: {
async: true,
},
properties: {
username: "",
password: "",
},
});