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

False positive errors for destructured arguments #98

Closed bitspook closed 8 years ago

bitspook commented 8 years ago
type TypeDateTime = {
    date: string,
    time: string
};

type TypeAction = {
    data: Object,
    name: string
};

seDateTime = ({date, time}: TypeDateTime) : TypeAction => {
    return {
        data: {
            date,
            time
        },
        name: 'DATE_TIME'
    };
};

Throws the error:

VM108950:68 Uncaught TypeError: Value of argument 0 violates contract.

Expected:
{ date: string;
  time: string;
}

Got:
{ date: string;
  time: string;
}

Which goes away if I make date and time in TypeDateTime optional.

Am I doing something wrong here or is this a bug?

phpnode commented 8 years ago

Looks like a bug introduced in the fix for #95.

For now, you can work around with:

// typecheck: ignore statement
seDateTime = ({date, time}: TypeDateTime) : TypeAction => {
    return {
        data: {
            date,
            time
        },
        name: 'DATE_TIME'
    };
};

I will try and fix the bug later today or tomorrow.

phpnode commented 8 years ago

Bug only affects arrow functions, another work around would be to use a normal function.

phpnode commented 8 years ago

fixed in 3.6.1