You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
foo type should be inferred as Foo, as described in the variable declaration
letfoo: Foo|null=null;/* ... */if(foo!=null){/* foo must be Foo type here */}
Actually, if IIFE used inference behave as expected
letfoo: Foo|null=null;(function(){foo={}})()if(foo!=null){/* foo is of type Foo here */}
Assertion function must be preferred over flow type inference
Actual behavior: foo type inferred as never and even assertion does not change it
Related Issues:
no
Code
functionassertNonEmpty<T>(value: T|null|undefined): asserts value is T{if(value==null){thrownewError("Should not be null or undefined");}}functionabsurd<Textendsnever>(value: T): asserts value is never{};typeAssertEqual<A,B>=AextendsB ? BextendsA ? true : never : never;functionrunCallback(cb: ()=>void){cb()};typeFoo={__TYPE__?: any};letfoo: Foo|null=null;runCallback(()=>foo={});if(foo!=null){constshouldBeInferredAsFoo: AssertEqual<typeoffoo,Foo>=true;assertNonEmpty<{}>(foo);constmustBeFoo: AssertEqual<typeoffoo,Foo>=true;absurd(foo);// foo actually is never}else{constnullish: AssertEqual<typeoffoo,null>=true;}
Output
"use strict";functionassertNonEmpty(value){if(value==null){thrownewError("Should not be null or undefined");}}functionabsurd(value){};functionrunCallback(cb){cb();};letfoo=null;runCallback(()=>foo={});if(foo!=null){constshouldBeInferredAsFoo=true;assertNonEmpty(foo);constmustBeFoo=true;absurd(foo);// foo actually is never}else{constnullish=true;}
TypeScript Version: 3.7.2
Search Terms:
callback, never, narrow, assert
Expected behavior:
Actually, if IIFE used inference behave as expected
Actual behavior:
foo
type inferred asnever
and even assertion does not change itRelated Issues:
no
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: