type Box = {readonly type: "GOOD", readonly value: string} | {readonly type: "BAD", readonly value: string}; async function coerce(t: string): Promise { let box: Box = { type: "GOOD", value: t }; let sentinel = { flag: false }; const p = new Promise(resolve => { const intervalId: IntervalID = setInterval(() => { if (sentinel.flag) { box = { type: "BAD", value: t }; clearInterval(intervalId); resolve(); } }, 1); }); if (box.type === "GOOD") { sentinel.flag = true; await p; if (box.type !== "GOOD") { return (box as empty).value; // error, but no underconstrained error } } throw new Error("Unreachable."); }