codemix / babel-plugin-typecheck

Static and runtime type checking for JavaScript in the form of a Babel plugin.
MIT License
886 stars 44 forks source link

Type annotation in loop #59

Closed sgodwin424 closed 8 years ago

sgodwin424 commented 8 years ago

In the following example:

let foo: Array<string> = [ ];

for (let bar: string of foo)
{
    // ...
}

I get a runtime error that bar violates its contract as it expected string got undefined. This only happens when foo is an empty array, but it seems a bit silly for this to occur. Running the same code through Flowtype does not produce a warning or error.

It is easy enough to just use ?string but this requires an additional null/undefined check before using bar so that Flowtype will not complain about it as well.

sgodwin424 commented 8 years ago

Fast fix. Thanks!

sgodwin424 commented 8 years ago

I have a case such as the following:

let foo: Array<string|Type> = [ ];

for (let bar: string|Type of foo)
{
    // ...
}

Where I receive the same error as before. I will take a look after Thanksgiving if you haven't gotten around to looking at it.

Thanks for the great plugin!