-
Notifications
You must be signed in to change notification settings - Fork 12.8k
unique symbol + interface + mixin causes TS4058 ("but cannot be named") #57165
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
Comments
You can turn off declaration emit and this should go away. What .d.ts for |
Yes it went away.
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; |
We don't traffic around the origination of 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. |
I'm having this problem with Project References and ppm, making me fall back to less safe opaque types. export type DatasetUid = string & { [datasetUidBrand]: true };
declare const datasetUidBrand: unique symbol; Causes the error: Return type of exported function has or is using name 'datasetUidBrand' from external module "/home/koss/code/mindcontrol/packages/db/schema" but cannot be named.ts(4058) So I have to use a public property: export type DatasetUid = string & { __brand: "DatasetUid" | undefined }; As a side note, I just came up with mixing in |
A working solution for unique types is export type AlphaSymbol = symbol & { readonly name: "AlphaSymbol" }
export type AlphaString = string & { readonly [k: AlphaSymbol]: true } You can just change the name to create another unique type export type BetaSymbol = symbol & { readonly name: "BetaSymbol" }
export type BetaString = string & { readonly [k: BetaSymbol]: true } You can then use them as unique types function f(x: AlphaString) {}
f("hello") // Error
f("hello" as AlphaString) // OK
f("hello" as BetaString) // Error You're welcome |
🔎 Search Terms
"unique symbol", "interface", "computed property", "mixin", "TS4058"
🕗 Version & Regression Information
⏯ Playground Link
No response
💻 Code
Workbench Repro
🙁 Actual behavior
The compiler emits an error at the definition of
function SomeMixin()
, saying:The problem disappears if you change the definition of
someUniqueSymbol
to:I believe this is wrong for 3 reasons:
someUniqueSymbol
cannot be named because it's exported byinterface.ts
.interface.ts
but notclass.ts
.SomeMixin()
does not mentionsomeUniqueSymbol
, or anything frominterface.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.
The text was updated successfully, but these errors were encountered: