gcanti / tcomb

Type checking and DDD for JavaScript
MIT License
1.89k stars 120 forks source link

Checking types in function parameters issue #325

Closed madenney closed 6 years ago

madenney commented 6 years ago

Version

3.2.25

import tcomb from "tcomb"

const typeCheck = tcomb.struct({
  name: tcomb.String,
  uuid: tcomb.String
})

const foo = ( userGroup: typeCheck ) => {
  // do stuff
}

const x = {
   name: "Joe",
   uuid: "test-test"
}

foo( x )

throws this error:

TypeError: [tcomb] Invalid value {
  "name": "Joe",
  "uuid": "test-test"
} supplied to userGroup (expected a Struct{name: String, uuid: String})

It works if I do the following:

const foo = ( userGroup ) => {
  typeCheck( userGroup )
  // do stuff
}

I'm really not sure what I'm doing wrong here.

gcanti commented 6 years ago

This is not valid JavaScript

//                       v---- a type?
const foo = ( userGroup: typeCheck ) => {
  // do stuff
}

I guess you are using babel-plugin-tcomb. If that's the case, try using tcomb.interface instead of tcomb.struct