Microsoft/TypeScript

Types intersected with string literal or 'unique symbol' error on computed property name declaration but only if it's abstract.

Open

#54.335 geöffnet am 21. Mai 2023

Auf GitHub ansehen
 (6 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)TypeScript (6.726 Forks)batch import
BugDomain: IntersectionHelp Wanted

Repository-Metriken

Stars
 (48.455 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)

Beschreibung

Bug Report

In the following code:

declare const aStringLiteral: 'string literal';

declare const aUniqueSymbol: unique symbol;

interface Whatever { p: any }

declare const objectWithToString: Whatever & typeof aStringLiteral;

declare const objectWithToPrimitiveSymbol: Whatever & typeof aUniqueSymbol;

objectWithToString and objectWithToPrimitiveSymbol are indeed of type string literal and 'unique symbol' respectively. So this expectedly works

class TestClass {
  [objectWithToString]() {} // works 🎉
  [objectWithToPrimitiveSymbol]() {} // works 🎉
}

But this fails, and the error message says the types aren't, even though they are.

abstract class TestAbstractClass {
  // Error: A computed property name in a method overload must refer to an expression whose type is a literal type or a 'unique symbol' type.(1168)
  abstract [objectWithToString](): void; // but IT IS a string literal type 🐛

  // Error: A computed property name in a method overload must refer to an expression whose type is a literal type or a 'unique symbol' type.(1168)
  abstract [objectWithToPrimitiveSymbol](): void; // but IT IS a 'unique symbol' 🐛
}

🔎 Search Terms

computed property abstract string literal type unique symbol

🕗 Version & Regression Information

⏯ Playground Link

Playground link with relevant code

💻 Code

In the description

🙁 Actual behavior

Works for non-abstract methods but fails for abstract methods.

🙂 Expected behavior

Should work for both kind of methods.

Contributor Guide