microsoft / microsoft/TypeScript

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

Open
#57,165 5 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Declaration Emit Help Wanted
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

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

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 three-file reproduction: interface.ts, class.ts, and mixin.ts, then inspect the compiler path that reports TS4058 for the exported SomeMixin return type. Compare the unique-symbol case with the string-literal case and the related issue #37888. Done means the reproduction compiles without the erroneous diagnostic and existing declaration-emission behavior remains covered.

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.