reactjs - 将 react js 文件转换为 tsx 文件
问题描述
将反应项目转换为打字稿时发生打字稿错误。
TypeScript error in /src/App.tsx(34,44):
No overload matches this call.
Overload 1 of 2, '(props: RouteProps | Readonly<RouteProps>): Route<RouteProps>', gave the following error.
Type '{ exact: true; path: string; name: string; render: (props: RouteComponentProps<any, StaticContext, unknown>) => Element; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Route<RouteProps>> & Readonly<RouteProps> & Readonly<...>'.
Property 'name' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Route<RouteProps>> & Readonly<RouteProps> & Readonly<...>'.
Overload 2 of 2, '(props: RouteProps, context: any): Route<RouteProps>', gave the following error.
Type '{ exact: true; path: string; name: string; render: (props: RouteComponentProps<any, StaticContext, unknown>) => Element; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Route<RouteProps>> & Readonly<RouteProps> & Readonly<...>'.
Property 'name' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Route<RouteProps>> & Readonly<RouteProps> & Readonly<...>'. TS2769
32 | <React.Suspense fallback={loading}>
33 | <Switch>
> 34 | <Route exact path="/login" name="Login Page" render={props => <Login {...props}/>} />
| ^
35 | <Route exact path="/register" name="Register Page" render={props => <Register {...props}/>} />
36 | <Route exact path="/404" name="Page 404" render={props => <Page404 {...props}/>} />
37 | <Route exact path="/500" name="Page 500" render={props => <Page500 {...props}/>} />
- git 克隆https://github.com/coreui/coreui-free-react-admin-template.git
- yarn add typescript (...with some other @types/...)
- 将 app.js 重命名为 app.tsx
- 纱线开始
- 得到打字稿错误
类型脚本似乎需要额外的代码(道具和状态)
所以我添加了界面道具和状态来解决上面的错误。
但是,它仍然不起作用。需要帮忙。
interface Props {};
interface State {};
const loading = (
<div className="pt-3 text-center">
<div className="sk-spinner sk-spinner-pulse"></div>
</div>
)
// Containers
const TheLayout = React.lazy(() => import('./containers/TheLayout'));
// Pages
const Login = React.lazy(() => import('./views/pages/login/Login'));
const Register = React.lazy(() => import('./views/pages/register/Register'));
const Page404 = React.lazy(() => import('./views/pages/page404/Page404'));
const Page500 = React.lazy(() => import('./views/pages/page500/Page500'));
const TestPage = React.lazy(() => import('./views/pages/testPage/TestPage'));
class App extends Component {
render() {
return (
<HashRouter>
<React.Suspense fallback={loading}>
<Switch>
<Route exact path="/login" name="Login Page" render={props => <Login {...props}/>} />
<Route exact path="/register" name="Register Page" render={props => <Register {...props}/>} />
<Route exact path="/404" name="Page 404" render={props => <Page404 {...props}/>} />
<Route exact path="/500" name="Page 500" render={props => <Page500 {...props}/>} />
<Route exact path="/test_page" name="Test Page" render={props => <TestPage {...props}/>} />
<Route path="/" name="Home" render={props => <TheLayout {...props}/>} />
</Switch>
</React.Suspense>
</HashRouter>
);
}
}
解决方案
您需要使用jsx
选项将 tsconfig.json 添加到您的项目中。
{
"compilerOptions": {
"target": "es5",
"module": "esnext",
"jsx": "react",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"allowSyntheticDefaultImports": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true
},
"include": [
"src"
]
}
然后你应该安装@types/react
,@types/react-dom
和@types/react-router-dom
之后,您应该调整组件的类型。
请记住,Route
没有name
财产。
这是Route的源代码(类型):
export interface RouteProps {
location?: H.Location;
component?: React.ComponentType<RouteComponentProps<any>> | React.ComponentType<any>;
render?: (props: RouteComponentProps<any>) => React.ReactNode;
children?: ((props: RouteChildrenProps<any>) => React.ReactNode) | React.ReactNode;
path?: string | string[];
exact?: boolean;
sensitive?: boolean;
strict?: boolean;
}
export class Route<T extends RouteProps = RouteProps> extends React.Component<T, any> {}
推荐阅读
- python - 是否有内置的模拟对象可以传递给 Python 单元测试?
- node.js - 退出时出现工作线程错误的电子应用程序
- ios - 如何在 iOS 中访问用户的日志信息?
- c++ - std::thread 是否对其构造函数返回时发生的情况做出任何保证?
- r - 对 data.frame 的每一行执行 lm(),其中预测器长度不同
- ruby - 什么 vscode ruby 扩展检测到未声明的变量之类的东西?
- javascript - 如果窗口宽度在 1000 像素以下超过 0.2 秒,是否可以重新加载网站?
- javascript - 将数值从一个 php 文件传递到另一个
- angular - Angular 9 服务使用 Observables/BehaviorSubject 按父 ID 返回结果
- html - 如何将 css 类应用于 ngFor 中的单个项目