microsoft / microsoft/TypeScript

unique symbol + interface + mixin causes TS4058 ("but cannot be named")

Aperta
#57,165 5 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: Declaration Emit Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 Search Terms

"unique symbol", "interface", "computed property", "mixin", "TS4058"

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Common "Bugs" That Aren't Bugs
⏯ Playground Link

No response

💻 Code
// @filename: interface.ts
export const someUniqueSymbol = Symbol.for("some-unique-symbol");

export interface ISomeInterface {
  [someUniqueSymbol]: string;
}

// @filename: class.ts
import { someUniqueSymbol, ISomeInterface } from "./interface";

export class SomeClass implements ISomeInterface {
  [someUniqueSymbol] = "some-value";
}

// @filename: mixin.ts
import { SomeClass } from "./class";

type Constructor<T> = abstract new (...args: any[]) => T;

// An error occurs here:
// Return type of exported function has or is using name 'someUniqueSymbol'
// from external module "file:///class" but cannot be named.(4058)
export function SomeMixin<T extends Constructor<SomeClass>>(base: T) {
  abstract class SomeMixin extends base {}
  return SomeMixin;
}

Workbench Repro

🙁 Actual behavior

The compiler emits an error at the definition of function SomeMixin(), saying:

Return type of exported function has or is using name 'someUniqueSymbol'
from external module "file:///class" but cannot be named.(4058)

The problem disappears if you change the definition of someUniqueSymbol to:

export const someUniqueSymbol = "some-string";

I believe this is wrong for 3 reasons:

  • There should be no reason why the type of someUniqueSymbol cannot be named because it's exported by interface.ts.
  • The message is wrong. It's from interface.ts but not class.ts.
  • The function SomeMixin() does not mention someUniqueSymbol, or anything from interface.ts at all, so it would be wrong to emit the error at the definition site of the said function.
🙂 Expected behavior

Compiles with no errors.

Additional information about the issue

This is similar to #37888 but I'm not sure if it's the same issue.

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 con la riproduzione composta da tre file: interface.ts, class.ts e mixin.ts, quindi esamina il percorso del compilatore che segnala TS4058 per il tipo restituito esportato SomeMixin. Confronta il caso unique-symbol con il caso string-literal e con l’issue correlata #37888. Il lavoro è completato quando la riproduzione viene compilata senza la diagnostica errata e il comportamento esistente di emissione delle dichiarazioni rimane coperto.

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.