Given the following type definition and function:
type SetResponseType = 'returnNumber' | 'returnString';
const argumentTypedFunction = (arg: SetResponseType) => {
if (arg === 'returnNumber') {
return 123;
}
return 'abc';
}
argumentTypedFunction('returnNumber'); // 123 | 'abc'
Why doesn't typescript knows that the return type could only be of type number when 'returnNumber' is used as argument? What do I have to do to make typescript infer the correct type depending on the parameter value?
CodePudding user response:
You can define your function using a function overload signature and get the inferred return type that you expect. Here's a code sample using the example data you showed in the question:
/** This signature returns a number when the parameter is "returnNumber" */
function argumentTypedFunction (param: 'returnNumber'): number;
/** This signature returns a string when the parameter is "returnString" */
function argumentTypedFunction (param: 'returnString'): string;
/** This is the implementation signature. It describes a combination of all possibilities */
function argumentTypedFunction (param: 'returnNumber' | 'returnString'): number | string {
return param === 'returnNumber' ? 123 : 'abc';
}
const num = argumentTypedFunction('returnNumber');
//^? const num: number
const str = argumentTypedFunction('returnString');
//^? const str: string
argumentTypedFunction('somethingElse'); /* Expected error