microsoft / microsoft/TypeScript

Cannot assign generic type aliases that should be equivalent.

Đang mở
#40,312 8 bình luận 1 reaction 1 người được giao Xem trên GitHub

@weswigham đang làm issue này rồi.

Từ ngày 1/9/2020.

Bug Domain: check: Variance Relationships
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.