flow
flow copied to clipboard
Options object destroys type checking.
A simple options object applied as a spread do an object seems to mess up type checking.
type Foo = { a: number, b: 'foo' }
const newFoo = (a: number, options?: {} = {}): Foo => ({
a,
b: 'foo',
...options
})
newFoo(1, { b: 'a' }) // No error!
Here's a Flow Try link.
I would expect it to throw an error. Am I doing something wrong?
I'm not sure why this doesn't trigger an error, but if you change the type of your options to $Shape<Foo> it will at least check that options has valid properties:
type Foo = { a: number, b: 'foo' }
const newFoo = (a: number, options?: $Shape<Foo> = {}): Foo => ({
a,
b: 'foo',
...options
})
// Error: Expected string literal `foo`, got `a` instead
newFoo(1, { b: 'a' });
Argh! I forgot about $Shape! Rats, that's exactly the utility method I was trying to figure out. Thanks @kevanstannard.