在 TypeScript 中使用 JS React 组件:JSX 元素类型“MyComponent”不是 JSX 元素的构造函数

Daw*_*ski 11 javascript node.js single-page-application typescript reactjs

我正在处理一个使用 React 框架的 JavaScript 遗留项目。我们定义了一些 React 组件,我想在一个完全不同的 TypeScript React 项目中重用它们。

JS React 组件在 control.jsx 文件中定义,如下所示:

export class MyComponent extends React.Component {
  render() {
    return <h1>Hi from MyComponent! Message provided: {this.props.message}</h1>;
  }
}
Run Code Online (Sandbox Code Playgroud)

在我的 TypeScript React 项目中,我试图这样使用它:

import * as React from "react";
import * as ReactDOM from "react-dom";
import { MyComponent } from "../JavaScriptProject/controls";

ReactDOM.render(
  <MyComponent message="some nice message"/>,
    document.getElementById("documents-tree")
);
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误: 在此处输入图片说明

错误消息说:

JSX 元素类型“MyComponent”不是 JSX 元素的构造函数。“MyComponent”类型缺少“ElementClass”类型中的以下属性:context、setState、forceUpdate、props 和另外 2 个。ts(2605) JSX 元素类不支持属性,因为它没有“props”属性。ts (2607)

我已经尝试过使用问题中描述的自定义类型文件的解决方案,但它没有任何改变。

我知道 JS 组件缺少 TypeScript 所需的一些强类型属性,但是在我的 tsconfig.json 中,我将allowJs设置为true

{
  "compilerOptions": {
    "allowJs": true,
    "baseUrl": ".",
    "experimentalDecorators": true,
    "jsx": "react",
    "noEmitOnError": true,
    "outDir": "lib",
    "sourceMap": true,
    "target": "es5",
    "lib": [
      "es2015",
      "dom"
    ]
  },
  "exclude": [
    "node_modules",
    "dist",
    "lib",
    "lib-amd"
  ]
}
Run Code Online (Sandbox Code Playgroud)

所以我希望它应该工作......

谢谢你的帮助

Dee*_*eep 0

以下是我在示例应用程序中使用的配置供您参考。

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": [
    "src"
  ]
}
Run Code Online (Sandbox Code Playgroud)

包.json

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.4.0",
    "@testing-library/user-event": "^7.1.2",
    "@types/jest": "^24.0.23",
    "@types/node": "^12.12.18",
    "@types/react": "^16.9.16",
    "@types/react-dom": "^16.9.4",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-scripts": "3.3.0",
    "typescript": "^3.7.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

我没有使用过webpack。