microsoft / microsoft/TypeScript

Typescript fail to unify type variables

Offen
#45,639 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Needs Investigation
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung


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`))

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere das Verhalten im verlinkten TypeScript Playground und vergleiche das explizite Typargument bei cons mit dem inferierten Aufruf in test. Beginne damit zu untersuchen, wie die generischen Typen cons, Logical und List interagieren; abgeschlossen ist die Aufgabe, wenn der nicht annotierte Aufruf mit dem erwarteten stringbasierten Typ akzeptiert wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.