Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is prototype in Typescript?

Tags:

typescript

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?

like image 219
Andrew Savinykh Avatar asked Oct 31 '22 21:10

Andrew Savinykh


1 Answers

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.

like image 78
C Snover Avatar answered Nov 15 '22 07:11

C Snover