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

v3.2.0 default parameters #62

Closed sgodwin424 closed 8 years ago

sgodwin424 commented 8 years ago

The 3.2.0 update seems to have messed up default parameters in some way. Specifically, the following example worked before the update, and fails after the update:

function foo(options?: {
        option1?: string,
        option2?: boolean
    } = { })
{
    // ...
}

The error message is:

Invalid default value for argument "options", expected { option1?: string;
  option2?: bool;
}.
  29 |         option1?: string,
  30 |         option2?: boolean
> 31 |     } = { })
     |       ^
  32 | {
  33 |
  34 | }

I have not tested more basic default parameter examples.

sgodwin424 commented 8 years ago

This seems to have partially fixed the issue. It does fix properties that can be undefined or a value (thus name?: ...), but it still fails to properly deal with nullable types (name: ?string). It gives the same error message as before.

I think adding && maybeNullableAnnotation(aprops.value) === false at https://github.com/codemix/babel-plugin-typecheck/blob/master/src/index.js#L725 will fix it, but I don't understand enough about the library to be sure.

I will add a pull request after Thanksgiving if you haven't already gotten around to fixing it.