microsoft / microsoft/TypeScript

Declare generics in type assertion / generic constraint and reuse

Offen
#41,033 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

Search Terms

generic, type assertion, narrowing, constraint, reuse

Suggestion

type Fn<T> = (subject: T) => void

const boolFn = fn as (<T extends boolean>Fn<T>)

// or a less flexible, but easier approach
const boolFn = fn as Fn<T extends boolean>

Today there is no way to do that.
The following does not work:

declare type boolFn<T extends boolean> = Fn<T>
const boolFn = fn

The current workaround is to redeclare or compose the base function:

const boolFn = function <T extends boolean>(subject: T) { return fn(subject) }

This unnecessary increase the size of the code.

Use Cases

There are cases where the functionality of a function only differs in their types.
To provide the best experience in TypeScript,
I would like to create some of the common variations of these function but using the same underlying function to keep the actual code size small.

Here is a simple example in type-plus:

export namespace assertType {
  export type Fn<T> = (subject: T) => void
}

export function assertType<T>(subject: T) { return }

assertType.isUndefiend = assertType as assertType.Fn<undefined> // ok
assertType.isBoolean = assertType as (<T extends boolean = boolean>assertType.Fn<T>) // propose

// usage
let subject: boolean | string = ...
assertType.isBoolean<false>(subject)

This proposal allows better reuse of generic types and provide better composability.
It also narrow the gap between the ability to type functions vs variables.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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

Der Vorschlag nennt keine Quelldateien, Tests oder Einstiegspunkte für die Implementierung. Beginne damit, die Compiler-Behandlung von type assertions, generic constraints und callable signatures zu lokalisieren; als abgeschlossen gilt die Aufgabe, wenn die vorgeschlagenen Beispiele einen type-check bestehen, ohne das emittierte JavaScript zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

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