Why doesn't Dart infer argument types in some scenarios where it can be determined from the function signature?

Viewed 45

I'm confused about when Dart will automatically infer a type. I've run into a scenario where I think Dart should be able to infer the type, but instead it considers it dynamic:

Stream<String> a = ...
Stream<String> b = ...
Stream<String> get c => a.withLatestFrom(b, (x, y) => x + y.thisDoesntExist);

From what I can tell in VS Code, x is recognized as String, but y is typed as dynamic. The compiler just lets me access non-existent properties on y, which is no good!

withLatestFrom comes from the rxdart package and is implemented like this:

Stream<R> withLatestFrom<S, R>(Stream<S> latestFromStream, R Function(T t, S s) fn) =>
      transform(WithLatestFromStreamTransformer.with1(latestFromStream, fn));

Looking at the signature of this function, it seems like Dart should be able to infer that y in my example is a string. If I specify the types on my function call (withLatestFrom<String, String>...) then y gets typed as a string correctly, and the code does not compile. For more complex data types in my app, this gets really verbose and specifying the types is undesirable.

Why can't Dart infer the types here?

0 Answers
Related