#Is or boolean

9 messages · Page 1 of 1 (latest)

thorn blaze
#

What's the difference between using is and boolean?

surreal jewel
#

and what's Is

thorn blaze
surreal jewel
#

is and boolean are completely different things

#

is is used to make type predicates, boolean is just a standalone type

#

your question doesn't make sense

nimble swan
#

If I would have to guess the direction of the question: You want to know the difference between the type of "boolean" and the type-predicate of "something is Class" I think the documentation may help you there: https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates

This is, as far as I understood, especially helpful for Tooling and Typesafety, because if that function returns the compiler can assume that the parameter given into that function IS indeed a type of asked class. Its just to ensure that the strict typing, or rather the function of child-class definitions, can be used on differently typed objects e.g. in a collection.

So you could have a collection of BaseObject and child-classes like Rectangle and Circle. Now a circle has a radius and the rectangle a width and height. Both have an area. If you would now have an Array of BaseObjects you could not retrieve the width or height of an reactangle nor the radius of a circle, but only the area. UNITL you define a method which will test for either of given type e.g. isCircle or isRectangle. WIthin the scope of the truthiness of such an if-block you can then access the methods of the corresponding sub-classes without the compiler naggig that those are not defined on BaseObjects, because it knows now that it is of that certain type.

#

I however would not say that my example is what one should write in a real application, but just to get the general idea over the wire 🙂

nimble swan
#

@thorn blaze