TypeScript error during method overriding: Overriding vs Overloading

Discussão em 'Angular' iniciado por Dhruv, Abril 16, 2025.

  1. Dhruv

    Dhruv Guest

    I have 2 classes I am using as services in angular 9: class A and class B, class B extends A

    class A{
    exportToCSV(data: any[], headers: any[]){
    ....
    }

    }


    class B extends A{
    exportToCSV(data: any[], headers: any[], metadata : any){
    ....
    }

    }


    It throws this error:


    Property 'exportToCSV' in type 'B' is not assignable to the same property in base type 'A'. Type '(data: any[], headers: any[], metadata : any) => void' is not assignable to type '(data: any[], headers: any[]) => void'.ts(2416)
    ​

    Based on the documents https://www.typescriptlang.org/docs/handbook/classes.html

    this should work, but it doesn't. If I make the metadata optional however, it works (https://medium.com/@kevinkreuzer/typescript-method-overloading-c256dd63245a).

    I am trying to override the method but instead it's overloading the method. Can someone explain why this is happening ?

    Continue reading...

Compartilhe esta Página