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

Provide stricter type checking on class constructors when interfaces are used as types for constructor arguments #2792

Closed
btipling opened this issue Nov 11, 2016 · 1 comment

Comments

@btipling
Copy link

The current compile time checking when using interfaces for arguments to constructors seems to be too lenient.

For example given the following class:

// @flow
'use strict';

import type { Bar } from './bar';

export default class Foo {

   _bar: Bar;
   _name: string;

   constructor (bar: Bar, name: string) {
     this._bar = bar;
     this._name = name;
   }
}

And the following interface defined in another place:

// @flow
'use strict';

export interface Bar {
  doSomething(someArg: string);
}

If I create an invalid instance of Foo with some kind of primitive type I'll get an error:


// In any of these flowtype checking works and fails because 
// it knows those things are not Bar.

new Foo('bar', 'someName'); 
new Foo(1, 'someName');
new Foo({}, 'someName');

But then if I do something silly like this:

new Foo(new Function(), 'someName');

flowtype is perfectly happy with this and that sort of defeats the purpose of even having defined an interface in the first place. If I can just pass in any kind of instance object and flowtype doesn't see that what's passed in does not match the interface it should throw an error just like it did for {}.

@vkurchatkin
Copy link
Contributor

See: #106

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants