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
TypeScript can currently infer the remainder of the type that has been narrowed down, without any issues:
constisError=<T>(value: T|Error): value is Error=>valueinstanceofError;constfoo=(value: string|number|Error)=>{if(isError(value)){returnvalue.message;// number, because of the type guard}else{returnvalue.toString();// Everything that is not a number.}}
When ! operator is used on the type predicate, the compiler casts it to boolean immediately.
constisError=<T>(value: T|Error): value is Error=>valueinstanceofError;typeSomething=number;typeSomethingElse=string;constgetSomething=(): Something|Error=>42;constgetSomethingElse=(): SomethingElse|Error=>Math.random()>0.5 ? 'foo' : Error('bugger');constloadsOfData=[getSomething(),getSomethingElse()];constallThingsFailed=loadsOfData.filter(isError);// Error[]constallThingsSuccessful=loadsOfData.filter(elem=>!isError(elem));// (string | number | Error)[]
Because of this, you can often end in situations, where you write a lot of unnecessary code:
constisError=<T>(value: T|Error): value is Error=>valueinstanceofError;constisNotError=<T>(value: T|Error): value is T=>!(valueinstanceofError);
Given that there seems to be the way TS represents a concept of Type less AnotherType, I'd be grand if the bang operator on type predicates worked the same way.
The text was updated successfully, but these errors were encountered:
Looks like a duplicate of #5101 (infer type predicate) and #18280 (negation types). elem => !isError(elem) is a new function expression and has no explicit type declaration; and a type predicate is never inferred.
Since there's no way to represent a !Error type currently, you may have to write a filterNot or partition helper.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
TypeScript can currently infer the remainder of the type that has been narrowed down, without any issues:
When
!
operator is used on the type predicate, the compiler casts it to boolean immediately.Because of this, you can often end in situations, where you write a lot of unnecessary code:
Given that there seems to be the way TS represents a concept of
Type less AnotherType
, I'd be grand if the bang operator on type predicates worked the same way.The text was updated successfully, but these errors were encountered: