Skip to content

Instantly share code, notes, and snippets.

@leonsenft
Last active October 27, 2017 22:39
Show Gist options
  • Save leonsenft/bd617ab891fca3f3f2815b0c85e7bfa1 to your computer and use it in GitHub Desktop.
Save leonsenft/bd617ab891fca3f3f2815b0c85e7bfa1 to your computer and use it in GitHub Desktop.
DartType resolveGenerics(DartType type) {
if (type is TypeParameterType) {
return type.resolveToBound(dynamicType); // T --> dynamic.
} else if (type is ParameterizedType) {
// [T, ...] --> [dynamic, ...]
final resolvedTypeArguments =
type.typeArguments.map(resolveTypeParameters).toList();
// Fails on nested unbounded type arguments.
// * List<T>.instantiate([dynamic]) --> List<dynamic>
// * List<Foo<T>>.instantiate([Foo<dynamic>]) --> List<Foo<T>>
return type.instantiate(typeArguments);
}
return type; // Nothing to resolve.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment