microsoft / microsoft/TypeScript

`satisfies` operator could not completely overshadow the existing contextual type

Abierto
#57,667 6 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Needs Proposal Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

🔍 Search Terms

satisfies contextual type implicit any parameters

✅ Viability Checklist
⭐ Suggestion

It would be great if satisfies wouldn't completely disassociate the expression from the existing contextual type.

📃 Motivating Example
declare function fn<T extends Record<string, (arg: number) => void>>(
  obj: T,
): void;

// this works without problems
fn({
  foo: (a) => {},
  bar: (a) => {},
});

// but we can't add extra constraints through `satisfies` without breaking existing contextual  typing
fn({
  foo: (a) => {}, // Parameter 'a' implicitly has an 'any' type.(7006)
  bar: (a) => {}, // Parameter 'a' implicitly has an 'any' type.(7006)
} satisfies Record<"foo" | "bar", unknown>);
💻 Use Cases
  1. What do you want to use this for?

It would be nice, at times, to add some extra constraints in places where the existing contextual type exists

  1. What shortcomings exist with current approaches?

satisfies breaks existing contextual typing so things like contextual parameters "break". It requires us to type the parameters manually or to introduce identity functions with those extra constraints like below

  1. What workarounds are you using in the meantime?

This is one of the possible workarounds:

declare function fn<T extends Record<string, (arg: number) => void>>(
  obj: T,
): void;

declare function identityConstraint<T extends Record<"foo" | "bar", unknown>>(
  arg: T,
): T;

fn(
  identityConstraint({
    foo: (a) => {},
    bar: (a) => {},
  }),
);

However, this has an unintended side-effect: excess property check doesn't apply to this object now.


A similar shortcoming related to ThisType and satisfies combination has been recently reported here.

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

Empieza reproduciendo el ejemplo motivador y compara la llamada ordinaria con la versión que usa satisfies. Lee la discusión relacionada en issue 57431 y sigue cómo interactúan el tipado contextual y satisfies. Se considera terminado cuando la restricción adicional se conserva sin causar errores de implicit-any en los parámetros del callback, mientras las comprobaciones existentes permanecen intactas.

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.