What is a prototype in Typescript?

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

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

What is the value of the prototype property on ErrorConstructor ?

+4
source share
1 answer

In TypeScript, there is no special value for the prototype property outside of the usual special value for the prototype property of JavaScript constructors.

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

In contrast, the signature new ErrorConstructor defines the type of object created when new called. The return value of the new constructor and prototype constructor is nominally of the same type, but JavaScript allows constructors to return values ​​that are not of their own type, so the difference is necessary for the correct set of all possible JavaScript code.

+2
source

All Articles