testcheck-js icon indicating copy to clipboard operation
testcheck-js copied to clipboard

[TypeScript] Can't use string, array or object as value in shape generator

Open sesm opened this issue 7 years ago • 2 comments

Documentation shows the following example for shape generator:

gen({
  type: 'Place',
  name: gen.string,
  location: [ gen.number, gen.number ],
  address: {
    street: gen.string,
    city: gen.string
  }
})

This works perfectly fine in plain JS in master.

But in TS:

gen({
  type: 'Place'
})

causes compilation error: Type 'string' is not assignable to type 'ValueGenerator<{}>'

gen({
  location: [ gen.number, gen.number ],
})

causes compilation error: Type 'ValueGenerator<number>[]' is not assignable to type 'ValueGenerator<{}>'.

gen({
  address: {
    street: gen.string,
    city: gen.string
  }
})

causes compilation error: Type '{ street: ValueGenerator<string>; city: ValueGenerator<string>; }' is not assignable to type 'ValueGenerator<{}>'.

sesm avatar Jun 12 '18 18:06 sesm

This looks like a more specific case of #79

sesm avatar Jun 12 '18 18:06 sesm

Found an easy workaround:

gen({
  type: 'Place',
  name: gen.string,
  location: [ gen.number, gen.number ],
  address: {
    street: gen.string,
    city: gen.string
  }
} as {})

sesm avatar Jun 13 '18 08:06 sesm