facebook / flow

Adds static typing to JavaScript to improve developer productivity and code quality.
https://flow.org/
MIT License
22.08k stars 1.85k forks source link

Flow doesn’t error for undefined in tuples #785

Closed iamdustan closed 8 years ago

iamdustan commented 9 years ago

When declaring a tuple type with expected types at each index, Flow doesn’t error on undefined slots. Doing the wrong type in a given index does error as expected.

Example repro at: https://gist.github.com/iamdustan/0fd4559b1339c0cb8759

/** @flow */

declare type ExampleTuple = [string, string, string];

function concat(ex:ExampleTuple): string {
  return ex.join(' ');
}

concat(['hello', 'world', 'golly']);

// I would expect flow to error on this line
concat(['hello', 'world']);

// I would expect this to error, too
var x:[string] = [];
minedeljkovic commented 8 years ago

No reaction to this so far?

I beleive this introduces some serious flaws in type checking, like:

  const x: [string] = [];
  const [y] = x;
  // calling concat on undefined, and it is allowed!
  const z = y.concat(''); // Uncaught TypeError: Cannot read property 'concat' of undefined

For example Typescript is strict with tuple:

const x: [string] = [];
// Typescript error: Type 'undefined[]' is not assignable to type '[string]'. Property '0' is missing in type 'undefined[]'.