Reactjs 如何在Typescript 2.8中使用Flux Container.create
我正在尝试将react/flux项目从typescript 2.2更新到2.8 我使用container.create实用程序函数定义了以下存储:Reactjs 如何在Typescript 2.8中使用Flux Container.create,reactjs,typescript,flux,Reactjs,Typescript,Flux,我正在尝试将react/flux项目从typescript 2.2更新到2.8 我使用container.create实用程序函数定义了以下存储: import * as React from 'react'; import { Container } from 'flux/utils'; export let MyContainer = Container.create(class extends React.Component<props, state> { stati
import * as React from 'react';
import { Container } from 'flux/utils';
export let MyContainer = Container.create(class extends React.Component<props, state> {
static getStores() {
return [/* list of stores here*/]
}
static calculateState(prevState: state, props: props): state {
return {hello: true}
}
render() {
return <div>something</div>
}
}, { withProps: true });
import*as React from'React';
从'flux/utils'导入{Container};
export let MyContainer=Container.create(类扩展React.Component{
静态getStores(){
返回[/*此处的门店列表*/]
}
静态属性(prevState:state,props:props):状态{
返回{hello:true}
}
render(){
归还某物
}
},{withProps:true});
如果导出此组件并尝试使用它,则会出现以下错误:
TS2605:JSX元素类型“Component”不是JSX元素的构造函数。
当我使用TypeScript2.2时,这工作得很好,但是在更新之后,我在尝试使用容器组件时,到处都会出现错误
我使用的是@types/react-typings版本15.0.28 显然,从typescript 2.6开始出现此错误。但我不确定是什么破坏性的改变导致了这种行为,似乎没有一个 根本原因是:
let readOnlyOrVoid: Readonly<void | {}> // resolves to void | Readonly<{}>
let readOnlyOfAny: Readonly<any> // in 2.5 this resolves to any, in 2.6 it does not we still have Readonly<any>
readOnlyOfAny = readOnlyOrVoid; // valid in 2.5, not so in 2.6
而JSX.element类
(扩展了React.Component
)的状态
的类型为只读
由于Container.create
将返回一个状态为ComponentState
的包装组件,因此我们遇到了将Readonly
(从包装组件)分配到Readonly
(从JSX.ElementClass
的定义)的问题
最简单的解决方案是将react
的打字更新为15.0.39
,这将组件状态的定义替换为:
type ComponentState = {};
尝试rm-rf node_模块和&rm warn.lock&&warn
从这里开始,我不使用warn,这不是安装在我的node_模块中的东西的问题directory@theycallmemorty,如果您愿意,您可以尝试使用npm本身,最好在出现升级场景时先尝试一下。您还可以确保您有“allowSyntheticDefaultImports”:在你tsconfig.json
中为true
?@TheyCallMemory,对我的评论或现有答案有任何反馈吗?感谢你的详细解释!!@TheyCallMemory别忘了奖励奖金:D
type ComponentState = {};