Open PetaSoft opened 4 years ago
I am also in favor of this... it would be great to have for my current project. I am running into an issue where I do not have intellisense for any methods located on the prototype for Object
, such as the common hasOwnProperty
and isPrototypeOf
. This can lead to misspellings and/or second guessing whether or not the method names even exist, which wastes time having to double check spelling/existence.
Search Terms
TypeScript 3.9, type declarations, Javascript classes, class hierarchy, object hierarchy
Suggestion
The TypeScript type declarations for the build in Javascript classes
Object
,Function
,Boolean
,Number
,String
,Symbol
, andBigInt
do not reflect the Javascript prototypical object hierarchy. All type declarations of these classes are implemented as interfaces which never use anextends
clause. There is no object hierarchy at all.As a concequence in VS Code IntelliSense does not display properties of parent objects of the prototype chain for a variable which is an instance of one of the mentioned classes.
Problems
1) Interfaces are not well suited to reflect object hierarchies as
interface DerivedName extends BaseName
means thatDerivedName
contains all the properties ofBaseName
. It does not say that there is a hierarchy but that there is an extended version of aninterface
. The type operatorkeyof
also merges the properties of both interfaces. 2) Using classes it is not possible to reflect the object hierarchy of the mentioned Javascript classes:Function
is not derived fromObject
and vice versa but there exists an object hierarchy between them. The type operatorkeyof
once again merges the properties of a base class with the properties of a derived class.Solution
Please add a new keyword (maybe
inherits
) similar toextends
andimplements
to express a hierarchy between interfaces without merging base interface members into the derived interface. Declaring a variable of such a derived interface type would only contain the members of the derived interface but IntelliSence would display also the members of the base interfaces. To reflect Javascript prototypical object hierarchies a derived interface should have a maximum of one base interface concering that hierarchy. The type operatorkeyof
should only reflect the properties of the derived interface (and not the base interface). As a result existing code should still work, and the emitted Javascript does not differ from the current Javascript code.Use Cases
1) VS Code IntelliSence would display all properties of the object hierarchy for a variable. 2) Build better type hierarchies in TypeScript which reflect object hierarchies in Javascript.
Examples
Checklist
My suggestion meets these guidelines: