I'm using Typescript and Enzyme for testing react components. I'm very new to Typescript.
I've got this helper function in tests:
const getComponent = (mountFn = shallow) => (
mountFn(<Component />)
)
This works when I run it as getComponent() but as soon I do getComponent(mount) it fails because typescript assumes getComponent returns ShallowWrapper.
I have a couple of questions:
- How do I tell Typescript that
mountFncan be eithershallowormount? - How do I tell it that the return value can be of type
ShallowWrapperorReactWrapper? - Ideally - how do I tell it that the return value should be of type
ShallowWrapperwhenshallowwas passed andReactWrapperwhenmountwas passed? - How do I do this using typings that are already defined in @types/enzyme?
- Is this a good practice at all? I used to do it all the time before using typescript but maybe I should just create 2 separate functions?