Type inference of function return value

  • Dart SDK Version: 2.0.0-dev.43.0
  • macOS

Considering a simple function like this:

add(int a, int b) {
  return a + b;

VSCode(with Dart extension) tells me the return type of add function is dynamic:

add(int a, int b) → dynamic

But if we add a wrong type, like

String add(int a, int b) {
  return a + b;

Then it shows an error:

[dart] The return type 'int' isn't a 'String', as defined by the method 'add'.

Obviously the compiler knows the return value is a int, but if we don’t declare it explicitly, it will be a dynamic. Is it by design?

Author: Fantashit

1 thought on “Type inference of function return value

  1. So perhaps having another analyzer flag to enable the stronger type inference as an opt-in would be possible in the future?

    I think that strict-inference will help in the case of the function return types and parameters, but its not implemented. It’s still an experimental flag, and I’ll be landing that support in the next month or two, so you could add this to your analysis-options, if you’d like to stay tuned 🙂

          strict-inference: true

Comments are closed.