-
Notifications
You must be signed in to change notification settings - Fork 231
Dart2: Collection literals in templates cause type cast failure #844
Description
Currently in DDC, binding a collection literal in a template where a typed collection is expected
<div [ngStyle]="{'color': divColor}">generates
Cast fail warning: Ignoring cast fail from IdentityMap<String, dynamic> to Map<String, String>
In Dart 2, this will become an error.
The culprit is the use of a pureProxyN function to change detect the bound values in the collection and build a new collection each time one changes.
While this function does have generic type parameters, the generated code doesn't use it in a context where they can be inferred, nor do we have the information to populate them manually.
This is an example of what the code generated for the above template looks like, with comments explaining the issue.
class ViewAppComponent0 {
NgStyle _NgStyle_0_4;
var _map_0;
var _expr_0;
ComponentRef<AppComponent> build() {
...
// There is no type from `_map_0`, nor the closure parameter, to flow
// through the type parameters of `pureProxy1`, thus the type returned by
// `_map_0` will be a `Map<String, dynamic>`.
_map_0 = pureProxy1((p0) {
return {'color': p0};
});
...
}
void detectChangesInternal() {
...
final currVal_0 = _map_0(_ctx.divColor);
if (!identical(_expr_0, currVal_0)) {
// The cast warning failure occurs here because `currVal_0` of type
// `Map<String, dynamic> is assigned to an input of type
// `Map<String, String>`.
_NgStyle_0_4.rawStyle = currVal_0;
_expr_0 = currVal_0;
}
...
}
}Type annotating the _map_0 field isn't always feasible, since it requires analyzing arbitrary expressions in the template. Likewise, determining the types bound in the collection pose a similar challenge.