microsoft / microsoft/TypeScript

Type logical operators "and", "or" and "not" in extends clauses for mapped types

Aperta
#31,579 8 commenti 4 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
1g 19h
PR unite (30g)
117

Descrizione

Search Terms

Pretty much the ones in the title...

Suggestion

I would like the ability to use logical operators for types.

I am aware that not is already planned as a feature, but I would like to make sure that this gets further extended to other logical operators, and I couldn't find any hints that this is in your minds already.

Use Cases

Type composition, better readability...

Examples

Current tricks to get to the desired behavior:

type Not<T extends boolean> = T extends true ? false : true

type Or<A extends boolean, B extends boolean> = A extends true
    ? true
    : B extends true
        ? true
        : false

type Or3<A extends boolean, B extends boolean, C extends boolean> = Or<A, Or<B, C>>

type And<A extends boolean, B extends boolean> = A extends true
    ? B extends true
        ? true
        : false
    : false

A few arbitrary use cases:

type Primitive = boolean | number | string | symbol | null | undefined | void
type IsA<T, E> = T extends E ? true : false

type IsIndexSignature<P> = Or<IsA<string, P>, IsA<number, P>>

type IsCallback<F extends Function> = F extends (...args: any[]) => any
    ? And<Not<IsA<Parameters<F>[0], Primitive>>, IsA<Parameters<F>[0], Event>> extends true
        ? true
        : false
    : false

All together in the Playground: here

Desired syntactic sugar to write the same:

type IsIndexSignature<P> = IsA<string, P> or IsA<number, P>

type IsCallback<F extends Function> = F extends (...args: any[]) => any
    ? not IsA<Parameters<F>[0], Primitive> and IsA<Parameters<F>[0], Event> extends true
        ? true
        : false
    : false

It would make the most sense to accompany this with better support for boolean checks in type definitions. That is, to allow to use the ternary operator directly without the constant need for extends true everywhere. Possibly, a new sort of "boolean type declaration" could be introduced, as to avoid having to propagate the boolean value all the way. For example, it should be possible to define KnownKeys (not full implementation here) like this:

type KnownKeys<T> = {
    [P in keyof T]: IsIndexSignature<P> ? never : T[P]
}

Without the need to do:

type KnownKeys<T> = {
    [P in keyof T]: IsIndexSignature<P> extends true ? never : T[P]
}

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

Inizia con gli esempi di Playground dell'issue e confronta la sintassi richiesta con il comportamento esistente di TypeScript per i tipi condizionali e mappati. Esamina come le definizioni dei tipi esprimono attualmente i risultati booleani e determina l'ambito del supporto per and, or e not nelle clausole extends. Il lavoro è completato quando gli esempi della proposta sono supportati senza modificare il JavaScript generato e con un'adeguata validazione del sistema dei tipi.

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
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.