Type test operators
The as
, is
, and is!
operators are handy for checking types at runtime.
Operator | Meaning |
as | Typecast (also used to specify library prefixes) |
is | True if the object has the specified type |
is! | True if the object doesn't have the specified type |
The result of obj is T
is true if obj
implements the interface specified by T.
Use the as
operator to cast an object to a particular type if and only if you are sure that the object is of that type.
(employee as Person).firstName = 'Bob';
If you aren't sure that the object is of type T
, then use is T
to check the type before using the object.
if (employee is Person) {
// Type check
employee.firstName = 'Bob';
}