这个错误非常明显。您只需要将名称空间声明移动到类之后。
import * as React from 'react';
import { connect } from 'react-redux';
import { RootState } from 'app/reducers';
@connect(
(state: RootState): LasarTitle.Props => {
return { title: state.title, loading: state.loading };
},
() => { }
)
export class LasarTitle extends React.Component<LasarTitle.Props> {
static defaultProps: Partial<LasarTitle.Props> = {
title: ''
};
constructor(props: LasarTitle.Props, context?: any) {
super(props, context);
}
render() {
const title = this.props.loading ? 'loading...' : this.props.title;
return (
<div>
<input readOnly={true} value={title} />
</div>
);
}
}
export namespace LasarTitle {
export interface Props {
title: string;
loading: boolean;
};
}
由于名称空间和类具有相同的名称,因此将合并它们。合并将要做的事情之一是,在运行时只有一个对象表示组合的类名称空间。出于实现的原因,这种合并要求首先创建类对象,然后向其中添加名称空间成员。
编辑
;
在接口的末尾,将被传输到空的JS语句,从而强制执行该规则。
更好的解决方案是移除
;
import * as React from 'react';
import { connect } from 'react-redux';
import { RootState } from 'app/reducers';
export namespace LasarTitle {
export interface Props {
title: string;
loading: boolean;
}
};
@connect(
(state: RootState): LasarTitle.Props => {
return { title: state.title, loading: state.loading };
},
() => { }
)
export class LasarTitle extends React.Component<LasarTitle.Props> {
static defaultProps: Partial<LasarTitle.Props> = {
title: ''
};
constructor(props: LasarTitle.Props, context?: any) {
super(props, context);
}
render() {
const title = this.props.loading ? 'loading...' : this.props.title;
return (
<div>
<input readOnly={true} value={title} />
</div>
);
}
}