microsoft / microsoft/TypeScript

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

Open
#39,068 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

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

  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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining the built-in declarations for Object, Function, Boolean, Number, String, Symbol, and BigInt, then trace how interface inheritance and keyof are handled. Compare the proposed hierarchy semantics with current IntelliSense behavior. Done means the declarations expose prototype-chain members without merging them into keyof, while existing code and emitted JavaScript remain unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.