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
I think type guards can be improved so that we don't need to define separate named function when we can provide lambda function.
Right now we don't need to abstract typeof and instanceof expressions into a function when we are dealing with single variable. For example:
functiongetStringOrNumber(): string|number{return"foo";}leta=getStringOrNumber();if(typeofa==="string"){a.concat("b")// "a" is string}else{a.valueOf();// "a" is number}// ------------------- //abstractclassAnimal{}classDogextendsAnimal{woof(): string{return"wooof"}}classCatextendsAnimal{meow(): string{return"meow"}}letanimal: Animal;if(animalinstanceofDog){animal.woof();// "animal" is Dog}elseif(animalinstanceofCat){animal.meow()// "animal" is Cat}
I expected to get similar behavior when using Array.filter method (the one with this signature: filter<S extends T>(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[];):
Expected behavior: filter result is array of narrowed type (string[], number[], Dog[], Cat[]) Actual behavior: filter result is array of the same type as original array ((string | number)[] and Animal[])
Proposed solutions
I have two proposals:
First
(preferable)
Improve type inference for function return type when using typeof and instanceof expression. So this code
would not get return type boolean but item is string, item is number and item is Cat
Second
(I'm not sure if I explained it properly)
When having anonymous function that is contextually typed to be type guard function and this function use typeof or instanceof expressions then it should infer the return type be type guard return type (arg is type).
Current behavior:
letfunc1: <T,SextendsT>(arg: T)=>arg is S=(arg : Animal)=>arginstanceofDog;letfunc2: <T,SextendsT>(arg: T)=>arg is S=(arg : string|number)=>typeofarg==="string";
Expected behavior:
It should "just work" and have return type be inferred as arg is Animal and arg is string . Actual behavior:
We get error:
'Type '(arg: Animal) => boolean' is not assignable to type '<T, S extends T>(arg: T) => arg is S'.
Signature '(arg: Animal): boolean' must be a type predicate.'
'Type '(arg: string | number) => boolean' is not assignable to type '<T, S extends T>(arg: T) => arg is S'.
Signature '(arg: string | number): boolean' must be a type predicate.'
Right now we need to explicitly say this anonymous function is type guard function.:
letfunc: <T,SextendsT>(arg: T)=>arg is S=(arg : Animal) : arg is Dog=>arginstanceofDog;letfunc2: <T,SextendsT>(arg: T)=>arg is S=(arg : string|number) : arg is string=>typeofarg==="string";letarrayOfStrings=arrayOfStringOrNumber.filter(<(arg: any)=>arg is string>(item=>typeofitem==="string"));letarrayOfNumbers=arrayOfStringOrNumber.filter(<(arg: any)=>arg is number>(item=>typeofitem==="number"));
The text was updated successfully, but these errors were encountered:
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
SUGGESTION
TypeScript Version: 2.5.2
Problem
I think type guards can be improved so that we don't need to define separate named function when we can provide lambda function.
Right now we don't need to abstract
typeof
andinstanceof
expressions into a function when we are dealing with single variable. For example:I expected to get similar behavior when using
Array.filter
method (the one with this signature:filter<S extends T>(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[];
):Expected behavior:
filter
result is array of narrowed type (string[]
,number[]
,Dog[]
,Cat[]
)Actual behavior:
filter
result is array of the same type as original array ((string | number)[]
andAnimal[]
)Proposed solutions
I have two proposals:
First
(preferable)
Improve type inference for function return type when using
typeof
andinstanceof
expression. So this codewould not get return type
boolean
butitem is string
,item is number
anditem is Cat
Second
(I'm not sure if I explained it properly)
When having anonymous function that is contextually typed to be type guard function and this function use
typeof
orinstanceof
expressions then it should infer the return type be type guard return type (arg is type
).Current behavior:
Expected behavior:
It should "just work" and have return type be inferred as
arg is Animal
andarg is string
.Actual behavior:
We get error:
Right now we need to explicitly say this anonymous function is type guard function.:
The text was updated successfully, but these errors were encountered: