microsoft / microsoft/TypeScript

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

Aperta
#53,906 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dalla riproduzione collegata in TypeScript Playground e confronta l'errore segnalato della proprietà calcolata con il comportamento equivalente di JavaScript. Traccia il modo in cui il compilatore verifica la proprietà denominata staticamente e l'unione delle dichiarazioni; il lavoro è concluso quando l'esempio viene compilato senza l'errore e il tipo di namespace unito rimane utilizzabile.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.