Narrow type by typeof value extends Type ? any : never.
declare const x :number | string;
if ( typeof x!=='string' ) { throw TypeError(); }
x;// string
Make inline type narrow code can be util function.
declare function throwNonString (value :unknown) :typeof value extends string ? void : never;
declare const x :number | string;
throwNonString(x);
x;// string
My suggestion meets these guidelines:
The syntax for this is
declare function throwNonString(value: unknown): asserts value is string;
@RyanCavanaugh Thank you! And I try to find this syntax in docs again, but I can't find it. Is it forgotten to write in docs?
@fatcerberus Thank you! So it's not in category docs yet... (Only in release notes)
Most helpful comment
The syntax for this is