microsoft / microsoft/TypeScript

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

Offen
#44,786 0 Kommentare 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.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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.

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

Beginne mit der Überprüfung der vorgeschlagenen Definition von Require<T, K extends keyof T> und ihrer IOptionalProps-Beispiele. Bestimme, ob der angeforderte Utility-Typ zum bestehenden Design der Utility-Typen von TypeScript passt, und identifiziere die passenden Compiler-Tests und die Dokumentation; abgeschlossen bedeutet, dass das Verhalten spezifiziert, getestet und, falls akzeptiert, dokumentiert ist.

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.