5

In lib.d.ts we can find the following piece of code:

interface Error {
    name: string;
    message: string;
}

interface ErrorConstructor {
    new (message?: string): Error;
    (message?: string): Error;
    prototype: Error;
}

declare var Error: ErrorConstructor;

What is the significance of the prototype property on ErrorConstructor?

1 Answer 1

2

There is no special significance of a prototype property in TypeScript beyond the normal special significance of the prototype property of JavaScript constructors.

In this code, the prototype property of the ErrorConstructor type/interface is set to ensure that any code that accesses ErrorConstructor.prototype directly will get the correct typing information for that property.

In contrast, the new signature of ErrorConstructor defines what the type of an object created with a new call will be. The new return value of a constructor and the prototype of the constructor are nominally the same types, but JavaScript allows constructors to return values that are not of their own type, so the distinction is necessary for correct typing of all possible JavaScript code.

Sign up to request clarification or add additional context in comments.

2 Comments

I might be wrong, but it appears to me that ErrorConstructor interface is not really meant to be used/referenced. It seems that it's there only so that we can "scaffold" type information around the external Error object. In this light, what possible practical reasons would be for accessing ErrorConstructor.proptotype?
Good point. I guess apart from exactly replicating what is defined in the ECMAScript standard (lib.d.ts is auto-generated after all), having prototype well typed could aid hand-coded extension of the Error class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.