【发布时间】:2018-12-11 23:23:36
【问题描述】:
我正在将现有的 React 代码迁移到 TypeScript,我遇到了很多问题,其中一个是当我创建 .js 文件时出现很多“找不到名称”错误 .ts 文件。
这里是有问题的代码:
import React from 'react';
const Footer = ({ children, inModal }) => (
<footer className={'tableBottomPanel' + (inModal ? " in-modal" : "") }>
<div>
{children}
</div>
</footer>
);
export default Footer;
从<footer> 到</footer> 的五行带有红色下划线,并给出各种错误,具体取决于我将鼠标悬停在哪里,例如:
- 找不到名称“页脚”。
- '>' 预期的
- 找不到名称“div”
- 未终止的正则表达式文字
- 运算符“
这是我的tsconfig.json 文件:
{
"compilerOptions": {
"outDir": "./dist/", // path to output directory
"sourceMap": true, // allow sourcemap support
"strictNullChecks": true, // enable strict null checks as a best practice
"module": "es6", // specify module code generation
"jsx": "react", // use typescript to transpile jsx to js
"target": "es5", // specify ECMAScript target version
"allowJs": true, // allow a partial TypeScript and JavaScript codebase
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"lib": [
"es6",
"dom"
],
"types": [
"node"
]
},
"include": [
"./src/"
]
}
我非常困惑,非常感谢一些帮助!
【问题讨论】:
-
你添加了 React/React DOM 的类型吗?另外,也许它应该命名为 .tsx。
-
此代码是否在
. tsx或. ts文件中?要使用jsx,扩展名必须是tsx
标签: reactjs typescript