typescript.d.ts

Type Native Javascript Error class with declarations


I want to type the native Class Error. The property cause in Error has the type unknown and I want it to be a enum I created myself.

The Error object look like this:

interface Error {
    name: string;
    message: string;
    stack?: string;
    cause: unkownwn;
}

I want to override cause using a declaration file, how can i do this?


Solution

  • You can't change the type of an existing property.

    You can add a property:

    interface A {
        newProperty: any;
    }
    

    But changing a type of existing one:

    interface A {
        property: any;
    }
    

    Results in an error:

    Subsequent variable declarations must have the same type. Variable 'property' must be of type 'number', but here has type 'any'

    You can of course have your own interface which extends an existing one. In that case, you can override a type only to a compatible type, for example:

    interface A {
        x: string | number;
    }
    
    interface B extends A {
        x: number;
    }