Search Terms
Suggestion
Narrow type by typeof value extends Type ? any : never.
Use Cases
declare const x :number | string;
if ( typeof x!=='string' ) { throw TypeError(); }
x;// string
Make inline type narrow code can be util function.
Examples
declare function throwNonString (value :unknown) :typeof value extends string ? void : never;
declare const x :number | string;
throwNonString(x);
x;// string
Checklist
My suggestion meets these guidelines:
Search Terms
Suggestion
Narrow type by
typeof value extends Type ? any : never.Use Cases
Make inline type narrow code can be util function.
Examples
Checklist
My suggestion meets these guidelines: