// With TS 3.7 function assertString(input: any): asserts input is string { // <-- the magic if (typeof input === 'string') return; else throw new Error('Input must be a string! // With TS 3.7 // Asserts that input is truthy, throwing immediately if not: function assert(input: any): asserts input { // <-- not a typo if (!input) throw new Error('Not a truthy value'); } declare const x: number | string | undefined; assert(x); // Narrows x to number | string // Also usable with type guarding expressions!