简体   繁体   English

在for循环中输入react + typescript?

[英]typing in a for loop react+typescript?

i have this interface of a Todo: 我有一个待办事项的界面:

export interface InitialTodoLoadingState {
  toggleComplete: boolean;
  updateText: boolean;
  deleteTodo: boolean;
}
export interface Todo {
  complete: boolean;
  _id: string;
  text: string;
  loading: InitialTodoLoadingState;
}

i am trying to loop an array of todos objects like this: 我正在尝试循环这样的待办事项对象数组:

const processing = todos // check if processing operations e.g.: toggle complete
      .map((todo: TodoInterface) => {
        for (let loadProp in todo.loading) {
          if (todo.loading[loadProp]) return true; // ERROR HERE
          return false;
        }
      })
      .some(process => !!process);

I am getting an error saying: 我收到一个错误消息:

Element implicitly has an 'any' type because type 'InitialTodoLoadingState' has no index signature.

how do i implement typescript here? 我如何在这里实现打字稿? I don't want to use any 我不想用任何

To remove the error, add an index signature (see here ): 要消除该错误,请添加索引签名(请参见此处 ):

export interface InitialTodoLoadingState {
  toggleComplete: boolean;
  updateText: boolean;
  deleteTodo: boolean;
  [key: string]: boolean;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM