microsoft / microsoft/TypeScript

Implement type Require to set specified Optional properties in an interface as Required

Aperta
#44,786 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Suggestion

TS interfaces such as Omit, Pick are doing great job helping us re-use current interfaces rather than re-writing them. One interface that I needed today was one to help me set specific property of another as Required. I did my search and it seems such a thing is not implemented. So, I tried and came up with this:


type Require<T, K extends keyof T> = T & { [P in K]: Required<P> };

interface IOptionalProps {
  prop1: string; // required by default
  prop2?: string // optional by default
  prop3?: string // //
}

const foo: Require<IOptionalProps, 'prop2'> = { prop1: 'foo', prop2: 'foo', }; // prop2 can't be left behind
const foobar: Require<IOptionalProps, 'prop2'> = { prop1: 'baz', prop2: 'baz', prop3: 'baz', }; //all props can't be left behind

🔍 Search Terms

  • interface require
  • interface required
  • transform optional required

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

  • Review the provided snippet.
  • Add it to TS if it goes well with the TS.

📃 Motivating Example

Provided as part of the suggestion.

💻 Use Cases

Provided as part of the suggestion.

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 esaminando la definizione proposta di Require<T, K extends keyof T> e i relativi esempi di IOptionalProps. Determina se il tipo utilità richiesto è coerente con il design esistente dei tipi utilità di TypeScript e individua i test del compilatore e la documentazione appropriati; il lavoro è completato quando il comportamento è specificato, testato e documentato se accettato.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.