isAssignableFromType method

bool isAssignableFromType(
  1. Type other
)

Determines whether this ResolvableType is assignable from the specified Type.

This method checks if a value of the specified Type can be assigned to a variable of this ResolvableType. It considers inheritance hierarchies and interface implementations.

Parameters:

  • other: The Type to check assignability from

Returns:

  • true if this type is assignable from the other type
  • false if assignment would not be valid

Example:

final objectType = ResolvableType.forClass(Object);
final stringType = ResolvableType.forClass(String);
final intType = ResolvableType.forClass(int);

print(objectType.isAssignableFromType(String)); // true - Object can hold String
print(stringType.isAssignableFromType(Object)); // false - String cannot hold Object
print(objectType.isAssignableFromType(int)); // true - Object can hold int

// Useful for type checking in generic contexts
bool canAssign<T>(Type sourceType, Type targetType) {
  final target = ResolvableType.forClass(targetType);
  return target.isAssignableFromType(sourceType);
}

Implementation

bool isAssignableFromType(Type other) {
  final resolved = resolve();
  if (resolved == null) return false;

  final otherClass = Class.forType(other);
  return resolved.isAssignableFrom(otherClass);
}