Skip to content

typeof on a well-known symbol in a computed property name has a poor error #42523

Closed
@DanielRosenwasser

Description

@DanielRosenwasser

Found talking to @benlesh

If you try to write the following

interface SymbolConstructor {
  observable: symbol;
}

declare function from<T>(obj: { [typeof Symbol.observable](): Observable<T> }): Observable<T>;
//                               ^^^^^^ Look at this use of 'typeof'.

type Observer<T> = (x: T) => void;

interface Observable<T> {
  subscribe(observer: Observer<T>): { unsubscribe(): void }
}

You'll get an error like

A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type.

This is pretty confusing, because we do support well-known symbols that are declared with symbol instead of unique symbol if you use the following syntax:

declare function from<T>(obj: { [Symbol.observable](): Observable<T> }): Observable<T>;
//                               ^ No 'typeof'.

It would be great if we could detect this case and specialize the error message with something like

Computed property names can only be named with well-known symbols without the 'typeof' operator. Try replacing `typeof {0}` with `{0}`.

Metadata

Metadata

Assignees

Labels

Domain: Error MessagesThe issue relates to error messagingEffort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Experience EnhancementNoncontroversial enhancementsFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions