microsoft / microsoft/TypeScript

Allow instantion expression with empty arguments

Offen
#57,510 2 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

In Discussion Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔍 Search Terms

instantiation expression, empty, type arguments

✅ Viability Checklist
⭐ Suggestion

I would like to be able to instantiate a generic function type with an empty type arguments list.

It would be nice to allow this for generic types, but it's not as necessary, since a generic type is always instantiated in a type context, even if no type arguments list is given. A generic function type needs to be specifically instantiated.

📃 Motivating Example

Here is a generic function where a type parameter has a default. I can get use an instantiation expression to specialize it to a non-generic function.

declare function find1<T, S extends T=T>(Ar:T[], predicate:(t:T)=>t is S): S | undefined;
type Find1Object = typeof find1<object>;

Here is a generic function with only one type parameter. I cannot use an instantiation expression with the default for S:

declare function find2<S extends object = object>(Ar:object[], predicate:(t:object)=>t is S) : S | undefined;
type Find2 = typeof find2<>; // <- this is a syntax error "Type argument list cannot be empty"
💻 Use Cases
  1. What do you want to use this for?
  2. What shortcomings exist with current approaches?
  3. What workarounds are you using in the meantime?
    • Declare an unused "dummy" type parameter so there's always a non-defaulted type argument.

Originally mentioned here: https://github.com/microsoft/TypeScript/issues/57463#issuecomment-1957739330

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Das Issue nennt keine Dateien oder Tests; beginne damit, die Parser- und Typprüfpfade für Instanziierungsausdrücke sowie die Diagnose zu finden, die eine leere Typargumentliste ablehnt. Verwende das find2-Beispiel als Akzeptanzfall: Eine leere Liste sollte akzeptiert werden und die generische Funktion anhand ihrer Standardwerte spezialisieren, ohne das ausgegebene JavaScript zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.