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

Imported type false-positive #126

Closed bitspook closed 8 years ago

bitspook commented 8 years ago

I have types defined in a module AppProps.js:

export type AppProps = {
    activeDate: Date,
    activeEntry: Object
};

On importing it in another module:

import {
    Map,
} from 'immutable';
import {
    AppProps,
} from '../typeDefinitions/AppProps';

export default (state : Map) : AppProps => {
    return {
        activeEntry: {},
        activeDate: state.get('activeDate'),
    };
};

It fails with runtime error:

Uncaught TypeError: Function return value violates contract.

Expected:
AppProps

Got:
{
  activeEntry: Object;
  activeDate: Date;
}

I am using babel v6.5.2 and babel-plugin-typecheck v3.7.0

Edit: It works fine if I keep the type definition in same module where it's used.

phpnode commented 8 years ago

@channikhabra paste your babelrc please, also see the notes in the readme regarding passPerPreset

bitspook commented 8 years ago

Hey,

Here's me .babelrc:

{
      "presets": [
            "es2015",
            "stage-0",
            "react"
      ],
    "plugins": [
        ["typecheck", {
            "disable": {
                "production": true
            }
        }]
    ],
      "env": {
            "development": {
                  "presets": ["react-hmre"]
            }
      }
}

Settings "passPerPreset": true make it fail with this error:

ERROR in ./src/app/index.js
Module build failed: TypeError: /Users/channi/Documents/Work/Dear-Diary/src/app/index.js: Property name of JSXOpeningElement expected node to be of a type ["JSXIdentifier","JSXMemberExpression"] but instead got "MemberExpression"
    at Object.validate (/Users/channi/Documents/Work/Dear-Diary/node_modules/babel-types/lib/definitions/index.js:101:13)
    at Object.validate (/Users/channi/Documents/Work/Dear-Diary/node_modules/babel-types/lib/index.js:269:9)
    at NodePath._replaceWith (/Users/channi/Documents/Work/Dear-Diary/node_modules/babel-traverse/lib/path/replacement.js:201:7)
    at NodePath.replaceWith (/Users/channi/Documents/Work/Dear-Diary/node_modules/babel-traverse/lib/path/replacement.js:179:8)
    at Object.ReferencedIdentifier (/Users/channi/Documents/Work/Dear-Diary/node_modules/babel-plugin-transform-es2015-modules-commonjs/lib/index.js:56:14)
    at Object.newFn (/Users/channi/Documents/Work/Dear-Diary/node_modules/babel-traverse/lib/visitors.js:326:17)
    at NodePath._call (/Users/channi/Documents/Work/Dear-Diary/node_modules/babel-traverse/lib/path/context.js:74:18)
    at NodePath.call (/Users/channi/Documents/Work/Dear-Diary/node_modules/babel-traverse/lib/path/context.js:46:17)
    at NodePath.visit (/Users/channi/Documents/Work/Dear-Diary/node_modules/babel-traverse/lib/path/context.js:104:12)
    at TraversalContext.visitQueue (/Users/channi/Documents/Work/Dear-Diary/node_modules/babel-traverse/lib/context.js:156:16)
 @ multi app
bitspook commented 8 years ago

Reordering presets to put react at top make the above error go away, but the issue with importing types still stands.

{
      "presets": [
            "react",
            "es2015",
            "stage-0"
      ],
    "passPerPreset": true,
    "plugins": [
        ["typecheck", {
            "disable": {
                "production": true
            }
        }]
    ],
      "env": {
            "development": {
                  "presets": ["react-hmre"]
            }
      }
}
bitspook commented 8 years ago

No luck re-ordering presets in any way (that passes compilation)

phpnode commented 8 years ago

@channikhabra very odd, I'll investigate this over the next day or two

gajus commented 8 years ago

@phpnode can this be assigned a label to track the importance?

ntkoopman commented 8 years ago

Isn't this because you are using import {AppProps} instead of import type {AppProps}?

bitspook commented 8 years ago

Thanks @ntkoopman for pointing it out. This was the reason apparently. It was silly of me to not pay proper attention there.

@phpnode Sorry for the inconvenience.