Skip to content

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

@PetaSoft

Description

@PetaSoft

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:

  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions