Hi CodeOzz, I might be totally wrong here, because I'm a Typescript noob, but your typeguard example doesn't look like the "Typescript way" to me. Today a friend of mine twittered some Typescript wisdom so now I want to propose the following typeguard:
As I understand it my (type based) version does type-checking at compile time while your (interface based) version does type checking only at runtime? Oh, and as you can see I also changed the return type of isFish to Boolean, because I don't understand the "pet is Fish" return type.
Hi Thorsten how are you ! Thank you for your comment !
I will try to explain you what is the pet is Fish. This none common synthax is called Type predicate in TS, it allow us to tell to TS that your param is typed as a Fish only if your function return true.
In my case I check if my object has swim methods, if it's the case I tell to TS -> Typescript, trust me, this pet is a Fish, not a Bird.
In you code, you put an interest thing, you create a new property in order to determine the type of your object. It should be a great idea but in your case you should delete this since you have already a way to determine the type of your object, thanks to the methods fly or swin, so you don't really need to add this new property !
About your function, it's not a really typeguard, and why? it's because your are correctly checking the type but the typescript checker is again doubting about the type of this object since you are not using predicated type !
Why it can be not really usefull to use the function like this? I will show you why of course !
consttoto=function(pet:Fish|Bird){if(isFish(pet)){// pet is again a Fish or a Bird for TSpet.swim()// error TS Property 'swim' does not exist on type 'Fish | Bird'.pet.fly()// error TS Property 'fly' does not exist on type 'Fish | Bird'.}}
So if you want to use pet in the if block, you cannot really use Fish property since for TS, pet is again a Fish or a Bird.
With the Type predicate, you don't have this issue:
consttoto=function(pet:Fish|Bird){if(isFish(pet)){// pet is a fish, since we tell to TS that pet is a Fish thanks to Typeguardpet.swim()// workpet.fly()// error TS Property 'fly' does not exist on type 'Fish', since pet is not a Bird !}}
But when you add new type Duck you will not be able to differentiate pets and fishes from Duck just checking available methods because a Duck can both swim and fly. In that case you will have to turn to Thorsten's explicit kind of an animal.
In the case of your interface growth you will need to use another way to put in the type guard yes. But in this exemple it's overkill to use more properties
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi CodeOzz, I might be totally wrong here, because I'm a Typescript noob, but your typeguard example doesn't look like the "Typescript way" to me. Today a friend of mine twittered some Typescript wisdom so now I want to propose the following typeguard:
As I understand it my (type based) version does type-checking at compile time while your (interface based) version does type checking only at runtime? Oh, and as you can see I also changed the return type of isFish to Boolean, because I don't understand the "pet is Fish" return type.
So what do you think?
Hi Thorsten how are you ! Thank you for your comment !
I will try to explain you what is the
pet is Fish
. This none common synthax is calledType predicate
in TS, it allow us to tell to TS that your param is typed as aFish
only if your function return true.In my case I check if my object has swim methods, if it's the case I tell to TS ->
Typescript, trust me, this pet is a Fish, not a Bird
.In you code, you put an interest thing, you create a new property in order to determine the type of your object. It should be a great idea but in your case you should delete this since you have already a way to determine the type of your object, thanks to the methods
fly
orswin
, so you don't really need to add this new property !About your function, it's not a really
typeguard
, and why? it's because your are correctly checking the type but thetypescript
checker is again doubting about the type of this object since you are not usingpredicated type
!Why it can be not really usefull to use the function like this? I will show you why of course !
So if you want to use pet in the if block, you cannot really use Fish property since for TS,
pet is again a Fish or a Bird
.With the
Type predicate
, you don't have this issue:If you have any question ask me ! ;D
Thank you. This predicate thingy seems pretty cool indeed. 👍
But when you add new type Duck you will not be able to differentiate pets and fishes from Duck just checking available methods because a Duck can both swim and fly. In that case you will have to turn to Thorsten's explicit kind of an animal.
In the case of your interface growth you will need to use another way to put in the type guard yes. But in this exemple it's overkill to use more properties