Consider the following example. fetchItems function returns response or response body depending on passed onlyBody argument which defaults to
Function overloads can do what you need:
function fetchItems(url: string, onlyBody: false): Promise>
function fetchItems(url: string, onlyBody?: true): Promise
function fetchItems(url: string, onlyBody: boolean = true) {
return Promise
.resolve({body: 'some data'} as any)
.then(res => onlyBody ? res.body : res);
}
Playground
Solution with conditional types does not work due to TypeScript "design limitation" described here.