React:在TS组件中使用ES6组件:TS2605:JSX元素类型'xxx'不是JSX元素的构造函数

问题描述 投票:3回答:1

我想在另一个Typescript组件中使用ES6 React组件。

ES6组件:

class ModelViewer extends Component {
  constructor(props, context) {
    super(props, context);
    //...
  }
// ...
}
export default ModelViewer;

我消耗的TS组件:

import * as ReactDOM from "react-dom";
import * as React from "react";
import ModelViewer from "./model_viewer/ModelViewer";

export class Viewer extends React.Component<any, any> {
    render() {
        return (
                <div>
                    <ModelViewer />
                </div>
        );
    }
}

TSC的选项是:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "removeComments": true,
        "sourceMap": true,
        "allowJs": true,
        "watch": true,
        "outDir": "./dist/",
        "jsx": "react",
        "experimentalDecorators": true,
        "allowSyntheticDefaultImports": true
    }
}

现在我收到错误TS2605: JSX element type 'ModelViewer' is not a constructor function for JSX elements.

尽管设置了"allowJS": true,我还需要提供打字吗?我尝试过不同的导入方式,但没有区别。

javascript reactjs typescript
1个回答
1
投票

作为一种强力方法,我使用自定义打字文件满足了Typescript编译器。

// typings/custom.d.ts
declare module "*";

并告诉我的tsconfig.json中的编译器读取我的自定义类型:

// tsconfig.json
{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "removeComments": true,
        "sourceMap": true,
        "allowJs": true,
        "watch": true,
        "outDir": "./dist/",
        "jsx": "react",
        "experimentalDecorators": true,
        "allowSyntheticDefaultImports": true,
        "typeRoots": [
             "./typings",
             "node_modules/@types"
         ]
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.