对于我要导入的一些模块,我最终修改它们的声明文件以使它们正常工作。我不确定我是否做错了,或者这些模块的类型是否刚好有问题。
index.tsx
import * as React from "react";
import * as moment from "moment";
import BigCalendar from "react-big-calendar";
import ReactModal from "react-modal";react和moment进口罚款。问题在于react-big-calendar和react-modal。这基本上就是我为react-modal所经历的过程
我安装了@types/react-modal。
node_modules/@types/react-modal/index.d.ts (简称)
// Type definitions for react-modal 1.6
// Project: https://github.com/reactjs/react-modal
// Definitions by: Rajab Shakirov <https://github.com/radziksh>, Drew Noakes <https://github.com/drewnoakes>, Thomas B Homburg <https://github.com/homburg>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
import * as React from "react";
export as namespace ReactModal;
export = ReactModal;
declare namespace ReactModal {
interface Props {
...
}
}
declare class ReactModal extends React.Component<ReactModal.Props> {
...
}我试过import ReactModal from "react-modal",这给了我Module '...' has no default export。
我试过import { ReactModal } from "react-modal",这给了我Module '...' has no exported member 'ReactModal'。
我尝试了import * asReactModal from "react-modal",它编译,但在运行时给我Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object。
最后,我将声明文件修改为:
import * as React from "react";
export as namespace ReactModal;
declare namespace ReactModal {
interface Props {
...
}
}
export default class ReactModal extends React.Component<ReactModal.Props> {
...
}然后我就可以使用import ReactModal from "react-modal"了,它成功了。
使用react-big-calendar,我只是卸载了@types/react-big-calendar,现在有了自己的类型。当然,其他人正在为这些包使用这些声明文件,而不是所有这些麻烦。如有任何指导,将不胜感激。
package.json
{
"dependencies": {
"@types/react": "^15.0.27",
"@types/react-dom": "^15.5.0",
"@types/react-modal": "^1.6.7",
"moment": "^2.18.1",
"react": "^15.5.4",
"react-big-calendar": "^0.14.0",
"react-dom": "^15.5.4",
"react-modal": "^2.1.0"
},
"devDependencies": {
"awesome-typescript-loader": "^3.1.3",
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"css-loader": "^0.28.4",
"source-map-loader": "^0.2.1",
"style-loader": "^0.18.2",
"typescript": "^2.3.4",
"webpack": "^2.5.0"
}
}tsconfig.json
{
"compilerOptions": {
"outDir": "./Scripts/dist/",
"sourceMap": true,
"noImplicitAny": true,
"target": "es5",
"jsx": "react"
},
"include": [
"./Scripts/src/**/*"
],
"exclude": [
"node_modules"
]
}发布于 2017-07-01 00:01:26
我认为反应-模态/索引.有一个bug,您(或我)应该提交一个拉请求。如果我正确地解释了他们的react-modal-tests.tsx,他们就会用编译但不运行的import * as ReactModal from 'react-modal';导入它。我的印象是他们的测试只是编译出来的。
我想出了如何使它工作,而不修改node_packages/@type中的文件。
将这些行添加到tsconfig文件的compilerOptions部分:"baseUrl": "src/types", "typeRoots": [ "./src/types", "./node_modules/@types" ]
然后将修改后的react-modal/index.d.ts放在src/types/react-modal/index.d.ts中。然后,类型记录编译器应该在那里找到它。我只是简单地将node_modules/@types/react-modal的内容复制到src/types/react-modal并进行了更改。
https://stackoverflow.com/questions/44803754
复制相似问题