如何在用reactjs构建的笑话测试中修复组件变量的类型推断?

问题描述 投票:0回答:2

我的场景是一个内置打字稿的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的推断?

reactjs typescript type-inference tsconfig
2个回答
1
投票

您将需要使用component的通用类型参数将ShallowWrapper变量键入为MyComponent。这是可以做到的:

import { shallow, ShallowWrapper } from 'enzyme';

let component: ShallowWrapper<MyComponentProps>;
component = shallow<MyComponentProps>(<MyComponent format="%MM : %SS" />)

0
投票

您可以尝试至少使用ShallowWrapper

© www.soinside.com 2019 - 2024. All rights reserved.