microsoft / microsoft/TypeScript

Support implicit invocations of `Symbol.toPrimitive` for statically-named properties

Open
#53,906 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

Original Title: Type declaration merging of a class, a namespace and another type like symbol or string

Given the following declaration

const ASymbol = Symbol('A');

class A {
  static [Symbol.toPrimitive]() {
    return ASymbol;
  }
}

namespace A {
  export type T = any;
}

I get an error when I do this:

class TestClass {
  [A]() { // error

  }
}

The why doesn't matter. It's a bug because Typescript is a "superset" of Javascript and it works in the latter.

It can be worked around with hacks like

const AWrapper: typeof A & typeof ASymbol = A as any;

class TestClass2 {
  [AWrapper]() { // works

  }
  otherMethod() {
    this[AWrapper](); // although this still gives an error
  }
}

But then I'm not able to do this:

let o: AWrapper.T; // error

It loses the the namespace properties.

Is it possible to merge all type declarations so that at least the hack can be used?

🔎 Search Terms

declaration merging class namespace type symbol string intersection

🕗 Version & Regression Information

Playground Latest v5+

⏯ Playground Link

Playground

💻 Code

In the description

🙁 Actual behavior

Error

🙂 Expected behavior

No error, or at least a way to hack around.

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 with the linked TypeScript Playground reproduction and compare the reported computed-property error with the equivalent JavaScript behavior. Trace how the compiler checks the statically named property and declaration merging; done means the example compiles without the error while the merged namespace type remains usable.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.