TypeScript requires that the return value of a type guard be a boolean, and nothing else. There's no room for any other information.
An assertion function has the same sort of problem - it must either throw or return void, nothing else.
If you want to extract any information other than the type narrowing you want, you'll have to do it elsewhere - such as
const result = isNumber(value);
if (!result) {
const reason = getReason(value);
// ...
}
I suppose you could assign to an outer scope variable to get what you want, but such constructions aren't functional and are often more difficult to get TypeScript to play well with.
let reason: string;
function isNumber(value: any): value is number {
if (typeof value === 'number') {
return true
} else {
reason = `Wrong typeof ${typeof value}`;
return false;
}
}