Typescript TS2345:类型为'的参数;承诺<;ReadonlyArray<;对象>&燃气轮机';不可分配给类型为';T |承诺式<;T>;|未定义';

Typescript TS2345:类型为'的参数;承诺<;ReadonlyArray<;对象>&燃气轮机';不可分配给类型为';T |承诺式<;T>;|未定义';,typescript,Typescript,我试图做的是在我的承诺中返回ReadOnlyRay,这样我就可以将它返回到最初的方法“dispatchToThisProcess”,我这样做的原因是抽象状态更新,以便将来可以将其更改为更新多个进程。下面是我的代码: private dispatchToThisProcess<T extends object>(action: Action): Promise<T> { return this.updateState(action.name, action) }

我试图做的是在我的承诺中返回ReadOnlyRay,这样我就可以将它返回到最初的方法“dispatchToThisProcess”,我这样做的原因是抽象状态更新,以便将来可以将其更改为更新多个进程。下面是我的代码:

private dispatchToThisProcess<T extends object>(action: Action): Promise<T> {
    return this.updateState(action.name, action)
  }
  // to be called by the dispatchToThisProcess
  private updateState<T extends object> (name: string, args: any): Promise<T> {
    return new Promise<T>((resolve, reject) => {
      console.log('In updateState, this replaced the send<T>(...) method')
      switch (name) {
        case 'add-repositories': {
          const addedRepos: ReadonlyArray<Repository> = new Array<Repository>()
          for (const path of args) {
            const addedRepo: any = Promise
            .resolve(this.repositoriesStore.addRepository(path))
            .then(result => addedRepo)
            addedRepos.join(addedRepo)
          }
          const returned = Promise.resolve(addedRepos)
          resolve(returned)
          break;
        }
        default: {
          reject()
          break;
        }
      }
    })
  }
private dispatchtothis流程(操作:操作):承诺{
返回此.updateEstate(action.name,action)
}
//由DispatchToThis进程调用
private updateState(名称:string,参数:any):Promise{
返回新承诺((解决、拒绝)=>{
log('在updateState中,这替换了send(…)方法')
交换机(名称){
案例“添加存储库”:{
const addedRepos:ReadonlyArray=新数组()
for(参数的常量路径){
const addedepo:any=承诺
.resolve(this.repositoriesStore.addRepository(路径))
.然后(结果=>addedRepo)
addedRepo.join(addedRepo)
}
const returned=Promise.resolve(addedRepos)
解决(返回)
打破
}
默认值:{
拒绝
打破
}
}
})
}
以下是我得到的错误:

 error TS2345: Argument of type 'Promise<ReadonlyArray<Repository>>' is not assignable to parameter of type 'T | PromiseLike<T> | undefined'.
  Type 'Promise<ReadonlyArray<Repository>>' is not assignable to type 'PromiseLike<T>'.
    Types of property 'then' are incompatible.
      Type '<TResult1 = ReadonlyArray<Repository>, TResult2 = never>(onfulfilled?: ((value: ReadonlyArray<Rep...' is not assignable to type '<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) |...'.
        Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
          Types of parameters 'value' and 'value' are incompatible.
            Type 'ReadonlyArray<Repository>' is not assignable to type 'T'.
错误TS2345:类型为“Promise”的参数不能分配给类型为“T | PromiseLike | undefined”的参数。
类型“PromiseLike”不可分配给类型“PromiseLike”。
“then”属性的类型不兼容。
类型“(OnCompleted:((值:ReadonlyArray TResult1 | PromiseLike)|…”。
参数“OnCompleted”和“OnCompleted”的类型不兼容。
参数“value”和“value”的类型不兼容。
类型“ReadonlyArray”不可分配给类型“T”。

我查了一下,有人说要添加T扩展对象,但这对我的情况没有帮助,我真的不知道从这里走到哪里。我使用的是TypeScript 2.4.0。

updateState只返回一个T。你可能希望它返回一个只读的T数组

  private updateState<T extends object> (name: string, args: any): Promise<ReadonlyArray<T>> {
    return new Promise<ReadonlyArray<T>>((resolve, reject) => {
private updateState(名称:string,参数:any):Promise{
返回新承诺((解决、拒绝)=>{

您使用什么作为T?类型为Repository的ReadonlyArray。Repository是一个对象。我仍然得到相同的错误,但这次它说:“错误TS2345:类型为'Promise'的参数不能分配给类型为'ReadonlyArray | PromiseLike | undefined'的参数。等等”我知道你想做什么,但有时我不会带着承诺返回数组,也许我只想返回一个对象。