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
?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With