microsoft / microsoft/TypeScript

Cannot assign generic type aliases that should be equivalent.

Ouverte
#40,312 8 commentaires 1 réaction 1 personne assignée Voir sur GitHub

@weswigham y travaille déjà.

Depuis le 1/9/2020.

Bug Domain: check: Variance Relationships
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

Cannot assign generic type aliases that should be equivalent.

Simple test case:

export type Example<TData>
    = (data: TData | null | undefined) => string | null;

function foo<T>()
{
    let x: Example<T> = undefined!;
    let y: Example<T | null | undefined> = undefined!;

    y = x;

    let x1: (data: T | null | undefined) => string | null = undefined!;
    let y1: (data: (T | null | undefined) | null | undefined) => string | null = undefined!;

    y1 = x1
}

Note that 'x' is not assignable to 'y' with strictNullChecks on. It results in the error:

Type 'Example<T>' is not assignable to type 'Example<T | null | undefined>'.
  Type 'T | null | undefined' is not assignable to type 'T'.
    'T' could be instantiated with an arbitrary type which could be unrelated to 'T | null | undefined'.
      Type 'undefined' is not assignable to type 'T'.
        'T' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.(2322)

This seems very strange though. Example is just a type alias, which shouldn't have an impact on assignability. If we simply expand the type alias, we get the x1/y1 case. In the expanded form the compiler allows this just fine.

Note: this was working in 3.3. It does not seem to work on 4.0. I'm not sure at what point it may have broken.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.