// *** before: ***
function assertIsString(val: unknown): void {
if (typeof val !== "string") {
throw new Error("Not a string!");
}
}
function process(val: unknown) {
assertIsString(val);
val.toUpperCase(); // <-Error: val is still unknown to the compiler
}
// *** in version 3.7: ***
function assertIsStringTyped(val: unknown): asserts val is string {
if (typeof val !== "string") {
throw new Error("Not a string!");
}
}
function processTyped(val: unknown) {
assertIsStringTyped(val);
val.toUpperCase(); // OK: after the call val is narrowed to string
}