我的场景是一个内置打字稿的React项目,特别是关于单元测试的项目。我正在将项目与tsconfig参数"noImplicitAny": true,
对齐我想知道如何解决如下所述的let组件问题。
let component
beforeEach(() => {
component = shallow(<MyComponent format="%MM : %SS" />)
})
describe('#render', () => {
it('shows the component correctly', () => {
expect(component.find('.my-component').exists()).toEqual(true)
})
})
错误
Variable 'component' implicitly has type 'any' in some locations where its type cannot be determined.ts(7034)
如何创建类型MyComponent
对变量component
的推断?
您将需要使用component
的通用类型参数将ShallowWrapper
变量键入为MyComponent
。这是可以做到的:
import { shallow, ShallowWrapper } from 'enzyme';
let component: ShallowWrapper<MyComponentProps>;
component = shallow<MyComponentProps>(<MyComponent format="%MM : %SS" />)
您可以尝试至少使用ShallowWrapper