Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
depressed-pho opened this issue Jan 25, 2024 · 3 comments
Open
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@depressed-pho
Copy link

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

@RyanCavanaugh
Copy link
Member

You can turn off declaration emit and this should go away.

What .d.ts for mixin.d.ts would you expect TS to emit in this case if it weren't an error?

@depressed-pho
Copy link
Author

You can turn off declaration emit and this should go away.

Yes it went away.

What .d.ts for mixin.d.ts would you expect TS to emit in this case if it weren't an error?

I would expect something like this:

import { SomeClass } from "./class";
type Constructor<T> = abstract new (...args: any[]) => T;
export declare function SomeMixin<T extends Constructor<SomeClass>>(base: T): (abstract new (...args: any[]) => {
    [Symbol.for("some-unique-symbol")]: string;
}) & T;

Or this:

import { SomeClass } from "./class";
type Constructor<T> = abstract new (...args: any[]) => T;
export declare function SomeMixin<T extends Constructor<SomeClass>>(base: T): (abstract new (...args: any[]) => {
    [import("./interface").someUniqueSymbol]: string;
}) & T;

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Feb 2, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Feb 2, 2024
@RyanCavanaugh
Copy link
Member

We don't traffic around the origination of Symbol.for calls, so the first one wouldn't work

Anyway this is supposed to be fixable by manually adding the import yourself, but that isn't working either. Something isn't working correctly here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

2 participants