Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect return values from functions do not throw. #70

Closed
8 tasks
chrbala opened this issue Mar 9, 2017 · 1 comment
Closed
8 tasks

Incorrect return values from functions do not throw. #70

chrbala opened this issue Mar 9, 2017 · 1 comment

Comments

@chrbala
Copy link

chrbala commented Mar 9, 2017

This is a:

  • [ X ] Bug Report
  • Feature Request
  • Question
  • Other

Which concerns:

  • [ X ] flow-runtime
  • babel-plugin-flow-runtime
  • flow-runtime-validators
  • flow-runtime-mobx
  • flow-config-parser
  • The documentation website

What is the current behaviour?

type StrFunType = () => string;
const strFun: StrFunType = () => 5;
strFun();

strFun() should throw because it did not return a string. Instead, I have to do the following in order to get it to throw:

type StrFunType = () => string;
const strFun: StrFunType = () => {
  const out: string = 5;
  return out;
};
strFun();

What is the expected behaviour?

Incorrect return values from functions should throw.

The above is a contrived example, but I've had to set the return value to a value of the expected type explicitly to get the type checking. My specific use case is that I'm using Ramda and would really like runtime flow checks on it because static flow type checks are not really going to happen for it.


Which package versions are you using?

version 0.7.0

@phpnode
Copy link
Collaborator

phpnode commented Mar 9, 2017

This happens because we can only reliably/safely check functions with annotations, otherwise there would be so many failures as to make the library totally impractical. This is also how flow does it in scenarios where it can't infer return types - it accepts Any<Function>. To get around this, you'll need to add your annotation to the function directly:

type StrFunType = () => string;
const strFun: StrFunType = (): string => 'abc'; // this is fine
const numFun: StrFunType = (): number => 123; // should throw

@phpnode phpnode closed this as completed Mar 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants