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
Currently only the is syntax with a bool return is supported.
Asserting on non-bool return should be possible to return the type one expects to avoid having to write separate assertion functions.
π Motivating Example
functiongetString<constT>(arg: T): Textendsstring ? T : false{if(typeofarg==='string'){returnarg;}returnfalse;}functionisString(val: any): val is string{returntypeofval==="string";}functiont1(arg: string|number): string{if(getString(arg)===false){consty=arg;// should be numberreturn'';}constz=arg;// should be stringreturn'';}functiont2(arg: string|number): string{constu=getString(arg);if(u===false){consty=u;return'';}constz=u;return'';}functiont3(arg: string|number): string{if(isString(arg)===false){consty=arg;return'';}constz=arg;return'';}
π» Use Cases
We already have a getString function in the codebase, which technically does assert something but typescript is unaware of it.
Now instead we have to write a separate is assert function which does the same thing, bloating the codebase unnecessary and increasing the maintenance burden.
You could argue that we should is functions only in the first place - however this would create lots of redundant code - where we could do just return getString(arg); we have to do return isString(arg) ? arg : false; which is the reason why there is "get" function in the first place.
The text was updated successfully, but these errors were encountered:
π Search Terms
assertion is typeguard
β Viability Checklist
β Suggestion
Currently only the
is
syntax with a bool return is supported.Asserting on non-bool return should be possible to return the type one expects to avoid having to write separate assertion functions.
π Motivating Example
π» Use Cases
We already have a getString function in the codebase, which technically does assert something but typescript is unaware of it.
Now instead we have to write a separate
is
assert function which does the same thing, bloating the codebase unnecessary and increasing the maintenance burden.You could argue that we should
is
functions only in the first place - however this would create lots of redundant code - where we could do justreturn getString(arg);
we have to doreturn isString(arg) ? arg : false;
which is the reason why there is "get" function in the first place.The text was updated successfully, but these errors were encountered: