microsoft / microsoft/TypeScript

[feature request] not sure how to word this: "infer generic arg type/constraint in wrapping type"

Abierto
#29,469 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

In Discussion Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

Search Terms

Suggestion

Sometimes we want to wrap an existing type, where the existing type has generic type args that have constraints using extends.

Use Cases

For example, this is material-ui's WithStyles type:


and I can wrap it like this to make one of it's properties optional:

// omit a set of keys from a type
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>

// like Partial, but choose which keys are optional instead of making all of them optional
type Optionalize<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>

// my wrapper type:
export type WithStyles_OptionalClasses<
  Style extends string | StyleRules | StyleRulesCallback = string,
  IncludeTheme extends boolean | undefined = false
> = Optionalize<WithStyles<Style, IncludeTheme>, 'classes'>

Examples

But it'd be nice if we could just pass along the args, not having to re-define them. Maybe it would look like the following, and achieve the same thing:

export type WithStyles_OptionalClasses<Style, IncludeTheme> =
  Optionalize<WithStyles<Style, IncludeTheme>, 'classes'>

and it would take advantage of the fact that WithStyles already has the constraint, so if we pass something into WithStyles_OptionalClasses that doesn't meet the constraint defined by WithStyles, then it could throw an error just like when using WithStyles by itself.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code not sure
  • 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. not sure

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

No se nombran archivos fuente, pruebas ni puntos de entrada del compilador. Empieza revisando los ejemplos de WithStyles, Optionalize y restricciones genéricas del issue; después, sigue el comportamiento de comprobación de tipos de TypeScript para los wrappers genéricos. El objetivo se alcanza cuando un wrapper puede transmitir los argumentos genéricos del tipo envuelto y hacer cumplir sus restricciones existentes sin cambiar el JavaScript generado.

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.