function funcOneCustom<T extends boolean = false>(isTrue: T) {
type RETURN = T extends true ? string : number;
return (isTrue ? "Nice" : 20) as RETURN;
}
function funcCbCustom<T>(cb: (isTrue: boolean) => T) {
const getFirst = () => cb(true);
const getSecond = () => cb(false);
return {
getFirst,
getSecond
};
}
const result = funcCbCustom((isTrueInCb) => {
return funcOneCustom(isTrueInCb);
});
I’m trying to figure out how to change the return types of funcCb automatically based on input.
const result: {
getFirst: () => string | number;
getSecond: () => string | number;
}
Is there a way to achieve the following without using 'as'?
const result: {
getFirst: () => string;
getSecond: () => number;
}
I want the return types to be inferred based on the function passed to funcCb. For example:
function funcTwoCustom<T extends boolean = false>(isTrue: T) {
type RETURN = T extends true ? Date : string;
return (isTrue ? new Date() : new Date().toString()) as RETURN;
}