Javascript React Redux类型脚本错误

Javascript React Redux类型脚本错误,javascript,reactjs,typescript,redux,react-redux,Javascript,Reactjs,Typescript,Redux,React Redux,尝试用React/Redux实现TypeScript,我的类型遇到了一些问题 我有以下几种: export interface IAuthState { isSignedIn: Boolean | null; userId: string | null; } export interface IStream { id: string; title: string; description: string; userId: string; } export interf

尝试用React/Redux实现TypeScript,我的类型遇到了一些问题

我有以下几种:

export interface IAuthState {
  isSignedIn: Boolean | null;
  userId: string | null;
}

export interface IStream {
  id: string;
  title: string;
  description: string;
  userId: string;
}

export interface IStreamState {
  streams: { [index: string]: IStream };
}
然后我有两个部分:

interface IStreamsProps {
  fetchStreams: () => void;
  streams: IStreamState;
  currentUserId: String | null;
  isSignedIn: Boolean | null;
  auth: IAuthState;
}

class StreamsList extends Component<IStreamsProps, IAppState> {
   // Error: The Property Map does not exist on type IStreamState
   renderList() {
       return this.props.streams.map((stream: IStream) => (// CODE)
   }
}


const mapStateToProps = (state: IAppState) => {
  return {
    streams: Object.values(state.streams),
    currentUserId: state.auth.userId,
    isSignedIn: state.auth.isSignedIn
  };
};

export default connect(
  mapStateToProps,
  { fetchStreams }
)(StreamsList);
如何解决这两个错误:

错误1:类型IStreamState上不存在属性映射


错误2:元素隐式具有“any”类型,因为“any”类型的表达式不能用于索引类型“IStreamState”

您错误地定义了
IStreamState
。仔细看。您已将其定义为对象

导出接口IStreamState{
流:{[index:string]:IStream}[];
}
//使用类型的等效声明:
导出类型IStreamState={
流:{[index:string]:IStream}[]
}
我确信您的意思是将其键入数组,如下所示:

export type IStreamState = { [index: string]: IStream }[]
编辑: 虽然与你的问题没有直接关系,但你需要小心你的类型。我注意到您在两个不同的地方使用了
IAppState
。类声明用于道具和本地状态
IAppState
似乎适用于您的redux商店

// the second generic argument is for `this.state`
class StreamsList extends Component<IStreamsProps, IAppState> {
//第二个通用参数是'this.state'`
类StreamsList扩展了组件{
编辑2: 在类中定义这两个参数是可选的。如果不定义第二个参数,则默认为
{}


不可能100%确定您的
MapStateTrops
中为什么会出现这个问题,因为我不知道
IAppState
是什么样子。只需返回两次并确认键入的
状态。streams
正是您所期望的。

您错误地定义了
IStreamState
。仔细看看。您已经将其定义为对象

导出接口IStreamState{
流:{[index:string]:IStream}[];
}
//使用类型的等效声明:
导出类型IStreamState={
流:{[index:string]:IStream}[]
}
我确信您的意思是将其键入数组,如下所示:

export type IStreamState = { [index: string]: IStream }[]
编辑: 虽然与你的问题没有直接关系,但你需要小心你的类型。我注意到你在两个不同的地方使用了
IAppState
。类声明用于道具和本地状态。
IAppState
似乎是用于你的redux商店

// the second generic argument is for `this.state`
class StreamsList extends Component<IStreamsProps, IAppState> {
//第二个通用参数是'this.state'`
类StreamsList扩展了组件{
编辑2: 在类中定义这两个参数是可选的。如果不定义第二个参数,则默认为
{}


你不可能100%确定你的
MapStateTrops
中为什么会出现这个问题,因为我不知道
IAppState
是什么样子。只要往回看一看,确认你键入的
状态。streams
正是你所期望的。你的问题是什么?你的问题是什么?哦,事实上,你很接近了,我t应该是这样的对象:
{id:{IStream},id:{IStream}
这是你的应用程序,所以我不确定数据结构是什么。根据代码,你的意图是将
流定义为一个数组,这似乎是合乎逻辑的。也就是说,有了这个上下文/信息,你自己能解决这个问题吗?是的,这有帮助,我将IStreamProps的界面更改为
流:IStreamState[]
。这解决了我在使用
.map
时遇到的错误。仍在查看第二个错误。也看到了您的编辑,我会查看一下。谢谢!关于您的编辑,我将签名更改为此
类StreamsList extends组件
,因为我没有使用本地状态。很高兴知道,我没有意识到这一点。仍在计算他的东西出来了。在你的第二期上更新了一点上下文哦,实际上,你很接近了,它应该是一个像这样的对象,
{id:{IStream},id:{IStream}
这是你的应用程序,所以我不确定数据结构是什么。根据代码,你的意图是将
流定义为一个数组,这似乎是合乎逻辑的。也就是说,有了这个上下文/信息,你自己能解决这个问题吗?是的,这有帮助,我将IStreamProps的界面更改为
流:IStreamState[]
。这解决了我在使用
.map
时遇到的错误。仍在查看第二个错误。也看到了您的编辑,我会查看一下。谢谢!关于您的编辑,我将签名更改为此
类StreamsList extends组件
,因为我没有使用本地状态。很高兴知道,我没有意识到这一点。仍在计算他的东西出来了。更新了你第二期的内容