简体   繁体   English

TypeScript错误:无效无法分配给参数类型

[英]TypeScript error: Void is not assignable to parameter type

I am getting this error in TypeScript: 我在TypeScript中遇到此错误:

Argument type (response: Response<DSBMannschaftDTO[]>) => void is not
  assignable to parameter type ((value:Response<DSBMannschaftDTO[]>) =>
  (PromiseLike<void> | void)) null | undefined | undefined

This is my schema: 这是我的架构:

export class DsbMannschaftDTO implements DataTransferObject {   
    id: number;   
    vereinId: number;   
    nummer: number;   
    benutzerId: number;   
    veranstaltungsId: number;   
    version: number;

    static copyFrom(optional: {
        id?: number,
        vereinId?: number,
        nummer?: number,
        benutzerId?: number;
        veranstaltungsId: number;
        version?: number  
    } = {}): DsbMannschaftDTO {
        const copy = new DsbMannschaftDTO();
        copy.id = optional.id || null;
        copy.vereinId = optional.vereinId || null;
        copy.benutzerId = optional.benutzerId || null;
        copy.veranstaltungsId = optional.veranstaltungsId || null;
        copy.nummer = optional.nummer || null;
        copy.version = optional.version || null;

        return copy;
    }
}

and this is my code 这是我的代码

private loadTableRows() {
    this.loading = true;

    this.dsbMannschaftDataProvider.findAll()
        .then(onfulfilled:(response: Response<DsbMannschaftDTO[]>) => this.handleLoadTableRowsSuccess(response))
        .catch(onrejected:(response: Response<DsbMannschaftDTO[]>) => this.handleLoadTableRowsFailure(response));
}

What exactly does this error mean? 该错误的确切含义是什么? Thank you very much in advance 提前非常感谢你

The problem here is your object definition. 这里的问题是您的对象定义。 You defined DsbMannschaftDTO like: 您将DsbMannschaftDTO定义DsbMannschaftDTO

id: number; <- value has to be a number
vereinId: number;   <- value has to be a number

and so on. 等等。 Now you pass the options object with the definition: 现在,您传递带有定义的options对象:

id?: number, <- number or undefined
vereinId?: number, <- number or undefined

this is the first missmatch to number . 这是第一个number匹配。 number | undefined number | undefined is not assignable to number . number | undefined不可分配给number

And then you write: 然后你写:

copy.id = optional.id || null; <- copy.id can now be number or undefined or null.

This is the second missmatch to number . 这是第二次来missmatch number number | undefined | null number | undefined | null is not assignable to number . number | undefined | null不可分配给number

To fix this, replace the null with 0 (better to say with a type matching default). 要解决此问题,请将null替换为0 (最好使用与默认类型匹配的类型)。

暂无
暂无

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

相关问题 TypeScript 错误 '(response) =&gt; void' 类型的参数不可分配给'(value: void) =&gt; void | 类型的参数承诺喜欢<void> '</void> - TypeScript error Argument of type '(response) => void' is not assignable to parameter of type '(value: void) => void | PromiseLike<void>' 打字稿错误:类型&#39;undefined []&#39;不能分配给&#39;void&#39;类型 - Typescript error: Type 'undefined[]' is not assignable to type 'void' TypeScript 错误:类型“void”不能分配给“boolean”类型 - TypeScript error: Type 'void' is not assignable to type 'boolean' Typescript 错误:TS2345:类型参数'(调度:调度)=&gt; Promise<void> ' 不可分配给“AnyAction”类型的参数</void> - Typescript error: TS2345: Argument of type '(dispatch: Dispatch) => Promise<void>' is not assignable to parameter of type 'AnyAction' 设置组件状态时出现 TypeScript 错误:“X”类型的参数不可分配给“() =&gt; void”类型的参数 - TypeScript error when setting a component's state: Argument of type 'X' is not assignable to parameter of type '() => void' void 不可分配给 &gt; '() =&gt; void' 类型的参数 - Void is not assignable to > parameter of type '() => void' 返回“void”类型参数的打字稿不能分配给“TimerHandler”类型的参数? - Typescript returning Argument of type 'void' is not assignable to parameter of type 'TimerHandler? Typescript 错误“类型参数不可分配给类型参数” - Typescript error 'argument of type is not assignable to parameter of type' moment.updateLocale格式无法分配给angular 4打字稿中的&#39;void&#39;类型的参数 - moment.updateLocale formats not assignable to parameter of type 'void' in angular 4 typescript Typescript 类型 boolean 不可分配给 void - Typescript type boolean is not assignable to void
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM