TestUtils findRenderedComponentWithType не скомпилируется, когда определен Компонент <Props> - PullRequest
0 голосов
/ 14 ноября 2018

Файл package.json:

{
  "scripts": {
    "test": "tsc --project . --noEmit"
  },
  "dependencies": {
    "@types/react": "^16.7.6",
    "@types/react-dom": "^16.0.9",
    "react": "^16.6.3",
    "react-dom": "^16.6.3",
    "typescript": "^3.1.6"
  }
}

Файл tsconfig.json:

{
  "compilerOptions": {
    "lib": ["es2015", "dom"],
    "target": "es2015",
    "moduleResolution": "node",
    "jsx": "react",
    "strict": true
  }
}

Файл test.tsx:

import * as React from 'react'
import * as ReactDOM from 'react-dom'
import * as TestUtils from 'react-dom/test-utils'

interface Props {
  a: number
}

class Component extends React.Component<Props> {
  render () {
    return 'test'
  }
}

const div = TestUtils.renderIntoDocument(
  <div>
    <Component a={1} />
  </div>
) as React.Component

const component = TestUtils.findRenderedComponentWithType(div, Component)

Запуск tsc --project . --noEmit приводит к следующей ошибке:

src/test.tsx:22:64 - error TS2345: Argument of type 'typeof Component' is not assignable to parameter of type 'ClassType<any, Component, ComponentClass<{}, any>>'.
  Type 'typeof Component' is not assignable to type 'ComponentClass<{}, any>'.
    Types of parameters 'props' and 'props' are incompatible.
      Type '{}' is not assignable to type 'Readonly<Props>'.
        Property 'a' is missing in type '{}'.

21 const component = TestUtils.findRenderedComponentWithType(div, Component)
                                                                  ~~~~~~~~~

Это ошибка? Если я отключу опцию --strict или пропущу <Props> в Component<Props>, он успешно скомпилируется.

1 Ответ

0 голосов
/ 15 ноября 2018

Объявления типов react-dom не предназначены для работы, когда включена опция strictFunctionTypes. У меня есть изменение , чтобы исправить проблему, которую вы видели, и несколько других, связанных с strictFunctionTypes. См. этот ответ о возможных способах использования моих измененных объявлений до их слияния с DefinitiveTyped.

...