我想在另一个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
,我还需要提供打字吗?我尝试了不同的导入方式,但没有区别。
答案 0 :(得分: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"
]
}
}