microsoft / microsoft/TypeScript

Resolve deferred conditional types to their `true` branch when instantiated with a type parameter constrained to the tested type

Abierto
#52,144 5 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Experience Enhancement Experimentation Needed Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

Bug Report

🔎 Search Terms

conditional-type, distributive-type, generics

🕗 Version & Regression Information

Version 4.9.4 and nightly

⏯ Playground Link

TS resolves type to different type even if I add constraint that should resolve to given type

💻 Code
type PrimitiveDataType = string | number | bigint | boolean | symbol | undefined | null;

type ConditionalType<T> = [T] extends [PrimitiveDataType]
  ? (v: T) => void
  : T extends Array<unknown>
  ? (v: T, t: number) => void
  : never;

abstract class AbstractClass<T> {
  abstract value: T;
  protected conditionalFunctions: Map<ConditionalType<T>, number | undefined> = new Map();
}

class SomeClass<T extends PrimitiveDataType> extends AbstractClass<T> {
  value: T;

  constructor(value: T) {
    super();
    this.value = value;
  }

  someMethod() {
    for (const someFn of this.conditionalFunctions.keys()) {
      someFn(this.value);
    }
  }
}

In the above code I have created a PrimitiveDataType which is a union of all primitive data types in JavaScript. Then I have created a ConditionalType<T> that will resolve to some callback only if T is one of PrimitiveDataType. Then I have created an abstract generic class that have a field which type(ConditionalType<T>) depends on generic value of this class. In the end, I have crated a SomeClass that extends AbstractClass<T> and add constraints that generic parameter T have to extend PrimitiveDataType.

🙁 Actual behavior

For someMethod of this SomeClass conditionalFunctions.keys() is resolved to (v: T, t: number) => void what would implicate that my generic T extends an Array type.

🙂 Expected behavior

For someMethod of this SomeClass I have expected that TS should resolve conditionalFunctions.keys() to (v: T) => void to my surprise it is resolved to (v: T, t: number) => void what would implicate that my generic T extends an Array type. This doesn't make sens to me because T has constraint that has to extends PrimitiveDataType.

Example of what I would expect:

Works for regular variables

type PrimitiveDataType = string | number | bigint | boolean | symbol | undefined | null;

type ConditionalType<T> = [T] extends [PrimitiveDataType]
  ? (v: T) => void
  : T extends Array<unknown>
  ? (v: T, t: number) => void
  : never;

const x: PrimitiveDataType = 12;
const y: ConditionalType<typeof x> = (param: number) => undefined;

StackOverflow question

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

Comienza con la reproducción enlazada en TypeScript Playground y compara el caso genérico de SomeClass con el ejemplo de la variable normal. Investiga la instanciación de tipos condicionales para un parámetro de tipo restringido a PrimitiveDataType. Se considera terminado cuando el caso restringido resuelve ConditionalType al callback de un argumento en lugar de a la rama de array, y ambos ejemplos se comportan de forma coherente.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
42/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.