The only time it makes sense to check if one type is a subtype of another type is when at least one of the types is a type variable. (Otherwise, you can just look at the source and write a constant true
or false
into the code).
There is a way to check whether one type is a subtype of another, and it does use the is
operator, but you need to have an instance as the first operand and a type as the second. You can't just create an instance of an unknown type, so we instead rely in Dart's covariant generics:
bool isSubtype<S, T>() => <S>[] is List<T>;
(You can use any generic class, or even create your own, instead of using List
. All it needs is a way to create the object.)
Then you can write:
print(isSubtype<TimeRecord, Record>()); // true!
Type
objects. There generally isn't much you actually can do withType
objects. What are you ultimately trying to do? There possibly is a better way to accomplish your goal. – Dardani