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

Destructuring and using a typed object won't perform a type check #95

Closed cesarandreu closed 8 years ago

cesarandreu commented 8 years ago

The following won't perform the type check on the destructured type and query object:

type GetThingParams = {
  type: string,
  query: string
};
export function getThing (fetch: Function, { type, query }: GetThingParams) {
  return fetch('/thing', {
    method: 'POST',
    body: JSON.stringify({ type, query })
  })
}

However, the following will:

type GetThingParams = {
  type: string,
  query: string
};
export function getThing (fetch: Function, params: GetThingParams) {
  const { type, query } = params
  return fetch('/thing', {
    method: 'POST',
    body: JSON.stringify({ type, query })
  })
}
vesln commented 8 years ago

:+1:

The test for the object patterns will pass regardless of the supplied input:

// the test
ok('object-pattern', {a: 'foo', b: 34});

// the fixture
export default function demo ({a, b}: {a: string, b: number}): string {
  return a;
}

// the transformed fixture
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = demo;
function demo(_ref2) {
  var a = _ref2.a;
  var b = _ref2.b;

  return a;
}

however, even if you change the test to the following, it will still pass:

ok('object-pattern', {a: 'foo', b: 'foo'});