writtenTypeArgumentIsSubtype function

bool writtenTypeArgumentIsSubtype(
  1. RuntimeType sub,
  2. RuntimeType sup
)

Whether a collection written with the type argument sub is one with the type argument sup (List<sub> is a List<sup>): Dart's subtype rule, stricter than _runtimeTypeCompatible, which takes a top type on EITHER side as a match. A recorded argument is what the script wrote, so <dynamic>[] is List<int> and <int?>[] is List<Object> are false as in Dart.

Implementation

bool writtenTypeArgumentIsSubtype(RuntimeType sub, RuntimeType sup) {
  final supName = sup.name;
  if (supName == 'dynamic' || supName == 'void' || supName == 'Object?') {
    return true;
  }
  final subName = sub.name;
  final subTop =
      subName == 'dynamic' || subName == 'void' || subName == 'Object?';
  if (supName == 'Object') {
    return !subTop && sub is! NullableRuntimeType && subName != 'Null';
  }
  if (subTop || subName == 'Object') return false;
  return _runtimeTypeCompatible(sub, sup);
}