microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.01k stars 12.49k forks source link

Adjust type declarations of build in Javascript classes to reflect Javascript object/class hierarchy #39068

Open PetaSoft opened 4 years ago

PetaSoft commented 4 years ago

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, and BigInt do not reflect the Javascript prototypical object hierarchy. All type declarations of these classes are implemented as interfaces which never use an extends 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 that DerivedName contains all the properties of BaseName. It does not say that there is a hierarchy but that there is an extended version of an interface. The type operator keyof 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 from Object and vice versa but there exists an object hierarchy between them. The type operator keyof 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 to extends and implements 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 operator keyof 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

// current situation:
type BooleanKeys1 = keyof Boolean;   // only "valueOf"
let boolValue1: Boolean = true;
// here IntelliSence only offers property valueOf and not properties of Object.prototype
boolValue1.           

// proposed situation:
interface Boolean inherits Object {
  valueOf(): boolean;
}
type BooleanKeys2 = keyof Boolean;   // only "valueOf"
let boolValue2: Boolean = true;
// here IntelliSence offers property valueOf and properties of Object.prototype
boolValue2.

Checklist

My suggestion meets these guidelines:

qdirks commented 1 year 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.