microsoft / microsoft/TypeScript

Generic Constraint using implements or equals

Aperta
#28,802 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

When working with types and one wants to capture generic typescript types and runtime information in one go, without using experimental decorators, which also has limitations when its comes to type categorization upfront, as it would require extract routines to evaluate if interface uses valid types subset of types when the interface is defined.

One doesn't want to evaluate at the level of string literals here, but at the level of primitive types.

What I would like to propose is a new key call it equal, which would check that the type equals or maybe implements, which perform the same logic as extends, but would not evaluate string | boolean
primitives at literal level or deconstructed level of that type, basically stop there. Almost like named typing in a way.

Search Terms

generic type implements instead of extends
generic implements

Suggestion

// This depends on who you read this statement, T equals boolean or string explicity
// T implements boolean | string pars

interface TSType<T equals boolean | string | number | null | undefined | readonly>
{
__tsType : T
}

type rr = TSType<true> // invalid type
type rr = TSType<false> // invalid type
type rr = TSType<'sdf'> // invalid type
type rr = TSType<234> // invalid type
type rr = TSType<234 | string> // invalid type
type rr = TSType<'sdf' | string> // invalid type
type rr = TSType<true | boolean> // invalid type




type rr = TSType<boolean> // valid types
type rr = TSType<string> // valid types
type rr = TSType<number> // valid types
type rr = TSType<undefined> // valid types
type rr = TSType<null> // valid types
type rr = TSType<readonly> // valid types
type rr = TSType<number | readonly> // valid types
type rr = TSType<string | number | readonly | undefined> // valid types

Extract routine for ensuring decorators class for type mixing , conform to specific set of constraints

This method, however, like all others for nested records and arrays, makes it a whole lot more complicate to evaluate the typings. I am actually think of proposing a symbol type, which has identifiers, for structure, which allows simpler ability to iterator nested record structures..

MeetsConstraints<T extends Record<string, any>, 
TNullable extends null | never,
TRequired extends undefined | never,
TReadonly extends readonly | never
//Default, but there is no mechanism for this, yet as not standard, would have use annotation type interrogation, if implemented in the future.
 = {
 [K in keyof T] T[K] implement TNullable | TRequired | TReadonly ? 'T' : never
}[keyof T]

// typically this would be extends constraint for some other class or generic
type valid = MeetsConstraints<...> extends 'T' ? 'good' : TError('bad')

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Non sono indicati file sorgente, test o punti di ingresso. Inizia chiarendo se equals o implements è previsto come nuovo vincolo generico e definiscine il comportamento per le unioni di primitivi e letterali, confrontandolo con i vincoli extends esistenti. Il lavoro è completato quando sono disponibili una specifica concordata e un piano corrispondente per il compilatore e i test.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
15/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.