I want to make a function that will try running provided function, and will throw unless it returns a truthy value, like this:
function test<T>(runnable: () => T): T {
const value = runnable();
if (! value) throw new Error(`Only truthy`);
return value;
}
const a = test(() => {
return (Math.random() < 0.5) ? undefined : "ok";
});
console.log(a);
How do I change the definition of test so that TS knows that a is always a string, not string|undefined?