Javascript 为什么这个使用排列运算符的类型脚本(w/React)没有编译?
我通过ES6 React/Redux应用程序进行转换,第一次运行Typescript。到目前为止,这有点困难 我想我和spread运营商之间有问题。我不能发布整个回购协议,但可以发布有问题的文件。FWIW,我在用react脚本-ts@2.3.2,使用Typescript 2.3.4。在2.3.3之前,我已经看到报告了关于spread的问题,但我认为这些问题都应该得到解决。这是我的档案:Javascript 为什么这个使用排列运算符的类型脚本(w/React)没有编译?,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我通过ES6 React/Redux应用程序进行转换,第一次运行Typescript。到目前为止,这有点困难 我想我和spread运营商之间有问题。我不能发布整个回购协议,但可以发布有问题的文件。FWIW,我在用react脚本-ts@2.3.2,使用Typescript 2.3.4。在2.3.3之前,我已经看到报告了关于spread的问题,但我认为这些问题都应该得到解决。这是我的档案: import React from 'react'; import { Router, Route, Swit
import React from 'react';
import { Router, Route, Switch } from 'react-router-dom';
import createHistory from 'history/createHashHistory';
import AppContainer from '../components/App/containers/AppContainer';
import { RouteTemplates } from './AppRouteUtility';
interface RouteTemplateType {
path: string;
component: new() => React.Component<RouteTemplateType>;
routes: Array<RouteTemplateType>;
}
let appRoute = {
path: RouteTemplates.AppRootTemplate,
component: AppContainer,
routes: []
} as RouteTemplateType;
export function makeRoutes(routes: Array<RouteTemplateType>) {
if (!routes) {
return;
}
return (
<Switch>{routes.map((r, i) => <Routes {...r} key={i} />)}</Switch>
);
}
export function Routes(routeConfig: RouteTemplateType) {
return (
<Route
path={routeConfig.path}
render={props => (
<routeConfig.component {...props} routes={routeConfig.routes} />
)}
/>
);
}
const historyObject = createHistory({
getUserConfirmation: (message, callback) => {
callback(window.confirm(message));
}
});
export function history() {
return historyObject;
}
export default class AppRouter extends React.Component<{}, {}> {
render() {
return (
<Router history={historyObject}>
<Routes {...appRoute} />
</Router>
);
}
}
从“React”导入React;
从“react Router dom”导入{Router,Route,Switch};
从“历史记录/createHashHistory”导入createHistory;
从“../components/App/containers/AppContainer”导入AppContainer;
从“./AppRouteUtility”导入{RouteTemplates};
接口RouteTemplateType{
路径:字符串;
component:new()=>React.component;
路由:阵列;
}
让批准={
路径:RouteTemplates.ApprotoTemplate,
组件:AppContainer,
路线:[]
}作为RouteTemplateType;
导出函数makeRoutes(路由:数组){
如果(!路由){
返回;
}
返回(
{routes.map((r,i)=>)}
);
}
导出功能路由(routeConfig:RouteTemplateType){
返回(
(
)}
/>
);
}
const historyObject=createHistory({
getUserConfirmation:(消息,回调)=>{
回调(窗口确认(消息));
}
});
导出函数历史记录(){
返回历史对象;
}
导出默认类AppRouter扩展React.Component{
render(){
返回(
);
}
}
以下是编译错误:
Failed to compile.
./src/routes/AppRouter.tsx
(13,16): error TS2352: Type '{ path: string; component: typeof default; routes: never[]; }' cannot be converted to type 'RouteTemplateType'.
Types of property 'component' are incompatible.
Type 'typeof default' is not comparable to type 'new () => Component<RouteTemplateType, {}>'.
Type 'default' is not comparable to type 'Component<RouteTemplateType, {}>'.
Types of property 'props' are incompatible.
Type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>' is not comparable to type 'Readonly<{ children?: ReactNode; }> & Readonly<RouteTemplateType>'.
Type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>' is not comparable to type 'Readonly<RouteTemplateType>'.
Property 'path' is missing in type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>'.
未能编译。
/src/routes/AppRouter.tsx
(13,16):错误TS2352:无法将类型“{path:string;component:typeof default;routes:never[];}”转换为类型“RouteTemplateType”。
属性“组件”的类型不兼容。
类型“typeof default”与类型“new()=>Component”不可比较。
类型“default”与类型“Component”不可比较。
属性“props”的类型不兼容。
类型“Readonly&Readonly”与类型“Readonly&Readonly”不可比较。
类型“Readonly&Readonly”与类型“Readonly”不可比较。
类型“Readonly&Readonly”中缺少属性“path”。
感谢您的帮助
更新
改变
interface RouteTemplateType {
path: string;
component: new() => React.Component<RouteTemplateType>;
routes: Array<RouteTemplateType>;
}
接口RouteTemplateType{
路径:字符串;
component:new()=>React.component;
路由:阵列;
}
到
接口RouteTemplateType{
路径:字符串;
component:new()=>React.component;
路由:数组|从不[];
}
通过了第一个错误。提到数组可能是空的,这真的有必要吗?我觉得有点疯狂
不过,我现在得到了这个错误,这似乎有点复杂,我相信与传播有关:
Failed to compile.
./src/routes/AppRouter.tsx
(34,40): error TS2322: Type '{ routes: never[] | RouteTemplateType[]; match: match<any>; location: Location; history: History;...' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<RouteTemplateType, {}>> & Readonly<{ chi...'.
Type '{ routes: never[] | RouteTemplateType[]; match: match<any>; location: Location; history: History;...' is not assignable to type 'Readonly<RouteTemplateType>'.
Property 'path' is missing in type '{ routes: never[] | RouteTemplateType[]; match: match<any>; location: Location; history: History;...'.
未能编译。
/src/routes/AppRouter.tsx
(34,40):错误TS2322:Type'{routes:never[]| RouteTemplateType[];match:match;location:location;history:history;…'不可分配给类型“intrinsitattributes&IntrinsicClassAttributes&ReadonlyMy行号可能已关闭,因为我删除了大量代码以进行复制。您的问题在于,在其他地方,使用数组或对象的扩展运算符可以正常工作。
Failed to compile.
./src/routes/AppRouter.tsx
(34,40): error TS2322: Type '{ routes: never[] | RouteTemplateType[]; match: match<any>; location: Location; history: History;...' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<RouteTemplateType, {}>> & Readonly<{ chi...'.
Type '{ routes: never[] | RouteTemplateType[]; match: match<any>; location: Location; history: History;...' is not assignable to type 'Readonly<RouteTemplateType>'.
Property 'path' is missing in type '{ routes: never[] | RouteTemplateType[]; match: match<any>; location: Location; history: History;...'.