microsoft / microsoft/TypeScript
Implement type Require to set specified Optional properties in an interface as Required
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza revisando la definición propuesta de Require<T, K extends keyof T> y sus ejemplos de IOptionalProps. Determina si el tipo utilitario solicitado encaja con el diseño existente de tipos utilitarios de TypeScript e identifica las pruebas del compilador y la documentación adecuadas; se considera terminado cuando el comportamiento está especificado, probado y documentado si se acepta.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 30/100