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

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

Closed
DanielRosenwasser opened this issue Jan 27, 2021 · 6 comments
Assignees
Labels
Domain: Error Messages The issue relates to error messaging Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Experience Enhancement Noncontroversial enhancements Fixed A PR has been merged for this issue

Comments

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jan 27, 2021

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}`.
@DanielRosenwasser DanielRosenwasser added Domain: Error Messages The issue relates to error messaging Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this Experience Enhancement Noncontroversial enhancements labels Jan 27, 2021
@jonhue
Copy link
Contributor

jonhue commented Jan 28, 2021

#42530 adds this error message 🙂

@benlesh
Copy link

benlesh commented Feb 11, 2021

Related... this also has issues (given any permutation of your example code):

// Imagine you got a reference to the symbol from somewhere else.
const symbolObservable = Symbol.observable;

const obj = {
    [symbolObservable](): Observable<any> {
        return null!;
    }
}

const x = from(obj);  // Error: Argument of type '{ [x: string]: () => Observable<any>; }' is not assignable to parameter of type '{ [Symbol.observable](): Observable<unknown>; [observable](): Observable<unknown>; }'.

@DanielRosenwasser
Copy link
Member Author

This is really just going to be closed by #24738 if I understand correctly.

@DanielRosenwasser
Copy link
Member Author

Or I guess #42543 now

@jonhue
Copy link
Contributor

jonhue commented Mar 7, 2021

@DanielRosenwasser just out of curiosity: was this fixed by #42543 and could be closed or should this remain open? 🙂

@DanielRosenwasser DanielRosenwasser added the Fixed A PR has been merged for this issue label Mar 8, 2021
@DanielRosenwasser
Copy link
Member Author

Yup, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Error Messages The issue relates to error messaging Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Experience Enhancement Noncontroversial enhancements Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants