microsoft / microsoft/TypeScript

Typescript fail to unify type variables

Abierto
#45,639 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

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

Descripción


name: Bug
about: Create a report to help us improve TypeScript
title: Typescript fail to unify type variables
labels: ''
assignees: ''

Bug Report

🔎 Search Terms

unify type variables, unification, type inference.

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about Generics.

⏯ Playground Link

Playground link with relevant code

💻 Code
class Var {
  id: number
  name: string

  static counter = 0

  constructor(name: string) {
    this.id = Var.counter++
    this.name = name
  }
}

function v(strs: TemplateStringsArray): Var {
  const [name] = strs
  return new Var(name)
}

type Logical<T> = Var | { [P in keyof T]: Logical<T[P]> }

type List<T> = null | { head: T; tail: List<T> }

function cons<T>(head: Logical<T>, tail: Logical<List<T>>): Logical<List<T>> {
  return { head, tail }
}

function test(element: Logical<string>, list: Logical<List<string>>): void {

}

// This is Ok.
test("a", cons<string>(v`element`, v`tail`))

// But this is not Ok.
test("a", cons(v`element`, v`tail`))
🙁 Actual behavior

When I do not write the annotation for cons, an error occured:

Argument of type 'Logical<List<{ id: { toString: ...; toFixed: ...; toExponential: ...; toPrecision: ...; valueOf: ...; toLocaleString: ...; }; name: { toString: ...; charAt: ...; charCodeAt: ...; concat: ...; indexOf: ...; lastIndexOf: ...; ... 43 more ...; at: ...; }; }>>' is not assignable to parameter of type 'Logical<List<string>>'.
  Type '{ head: Logical<{ id: { toString: ...; toFixed: ...; toExponential: ...; toPrecision: ...; valueOf: ...; toLocaleString: ...; }; name: { toString: ...; charAt: ...; charCodeAt: ...; concat: ...; indexOf: ...; lastIndexOf: ...; ... 43 more ...; at: ...; }; }>; tail: Logical<...>; }' is not assignable to type 'Logical<List<string>>'.
    Type '{ head: Logical<{ id: { toString: ...; toFixed: ...; toExponential: ...; toPrecision: ...; valueOf: ...; toLocaleString: ...; }; name: { toString: ...; charAt: ...; charCodeAt: ...; concat: ...; indexOf: ...; lastIndexOf: ...; ... 43 more ...; at: ...; }; }>; tail: Logical<...>; }' is not assignable to type '{ head: Logical<string>; tail: Logical<List<string>>; }'.
      Types of property 'head' are incompatible.
        Type 'Logical<{ id: { toString: ...; toFixed: ...; toExponential: ...; toPrecision: ...; valueOf: ...; toLocaleString: ...; }; name: { toString: ...; charAt: ...; charCodeAt: ...; concat: ...; indexOf: ...; lastIndexOf: ...; ... 43 more ...; at: ...; }; }>' is not assignable to type 'Logical<string>'.
          Type '{ id: Logical<{ toString: unknown; toFixed: unknown; toExponential: unknown; toPrecision: unknown; valueOf: unknown; toLocaleString: unknown; }>; name: Logical<{ toString: unknown; charAt: unknown; charCodeAt: unknown; concat: unknown; indexOf: unknown; lastIndexOf: unknown; localeCompare: unknown; ... 42 more ...; ...' is not assignable to type 'Logical<string>'.
            Type '{ id: Logical<{ toString: unknown; toFixed: unknown; toExponential: unknown; toPrecision: unknown; valueOf: unknown; toLocaleString: unknown; }>; name: Logical<{ toString: unknown; charAt: unknown; charCodeAt: unknown; concat: unknown; indexOf: unknown; lastIndexOf: unknown; localeCompare: unknown; ... 42 more ...; ...' is not assignable to type 'Var'.
              Types of property 'id' are incompatible.
                Type 'Logical<{ toString: unknown; toFixed: unknown; toExponential: unknown; toPrecision: unknown; valueOf: unknown; toLocaleString: unknown; }>' is not assignable to type 'number'.
                  Type 'Var' is not assignable to type 'number'.

37 test("a", cons(v`element`, v`tail`))
             ~~~~~~~~~~~~~~~~~~~~~~~~~
🙂 Expected behavior

I expect type checker inference the type annotation for me, so I can write:

test("a", cons(v`element`, v`tail`))

Instead of:

test("a", cons<string>(v`element`, v`tail`))

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

Reproduce el comportamiento en el TypeScript Playground enlazado y compara el argumento de tipo explícito en cons con la llamada inferida en test. Empieza examinando cómo interactúan los tipos genéricos cons, Logical y List; la tarea estará terminada cuando se acepte la llamada sin anotación con el tipo esperado basado en string.

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
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.