microsoft / microsoft/TypeScript

Allow conditionally setting optional properties in a mapped type

Aperta
#36,126 8 commenti 15 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

Search Terms

conditional optional mapped type

Suggestion

Allow us to conditionally apply ? to a property in a mapped type

Use Cases

I have a function:

function myFunction(one: string, two: number, three: number){}

but I want to map the params to

function myFunction(one: string, two?: number, three?: number){}

Examples

Suggested approach:

type MapParams<T> = {
    [P in keyof T]: T[P] extends string ? T[P] : | ?T[P]
}

(or something similar)

This is the closest I could come:

type MapParams<T> = {
    [P in keyof T]: T[P] extends string ? T[P] : | T[P] | undefined
}

but that produces

function myFunction(one: string, two: number | undefined, three: number | undefined){}

myFunction("", undefined, undefined)

Playground

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

La issue non fornisce file di implementazione né test; inizia con il Playground collegato e gli esempi di mapped types. Il completamento consiste nel supportare un modo per rendere condizionalmente opzionali determinate mapped properties, distinguendo le properties omesse da quelle impostate esplicitamente su undefined, con test del compilatore che coprano la forma della funzione mostrata.

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
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.