-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Description
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
- Interfaces are not well suited to reflect object hierarchies as
interface DerivedName extends BaseNamemeans thatDerivedNamecontains all the properties ofBaseName. It does not say that there is a hierarchy but that there is an extended version of aninterface. The type operatorkeyofalso merges the properties of both interfaces. - Using classes it is not possible to reflect the object hierarchy of the mentioned Javascript classes:
Functionis not derived fromObjectand vice versa but there exists an object hierarchy between them. The type operatorkeyofonce 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
- VS Code IntelliSence would display all properties of the object hierarchy for a variable.
- 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:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.