microsoft / microsoft/TypeScript

Contextual typing fails to throw error with function return type on left side of equals sign

Đang mở
#38,879 4 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Bug Domain: check: Contextual Types
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ả

TypeScript Version: I got the same behavior on 3.7.4 and 4.0.0-dev.20200601

Search Terms:
left side
right side
contextual typing
type inference

Code

interface ReportState {
  reportDefinition: {
    [key: number]: ReportDetails;
  };
}

interface ReportDetails {
  requiredNumber: number;
  columnWidths?: number[];
}

export const Report: (state: ReportState, action: any) => ReportState = (
  state,
  action
) => { // See note below about this line
  switch (action.type) {
    case "TYPE0": {
      const { reportDef, sequenceId } = action.payload;
      return {
        ...state,
        reportDefinition: {
          ...state.reportDefinition,
          [sequenceId]: {
            ...reportDef,
            selectedElements: ["0x0"],
          },
        },
      };
    }

    case "TYPE1": {
      return {
        ...state,
        reportDefinition: {
          ...state.reportDefinition,
          [action.payload.sequenceId]: {
            // requiredNumber is missing here. This should be an error.
            columnWidths: action.payload.columnWidths,
          },
        },
      };
    }

    default:
      return state;
  }
};

Expected behavior:
There should be an error, because requiredNumber is missing from the second return statement.

Actual behavior:
Compilation succeeds without error.

Curiosity 1:
If you add the return type on the right side of the =, i.e. by changing
) => {
to
): ReportState => {
, it correctly finds the error. This should not be necessary, because the return type is already specified on the left side of the =.

Curiosity 2:
If you remove the first case block, it correctly finds the error (which is in the second case block).

Playground Link: Click here

Related Issues:
No

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.

Hướng nghiên cứu

Bắt đầu với TypeScript Playground được liên kết và tái hiện ví dụ bằng cách sử dụng kiểu trả về của hàm ở phía bên trái dấu bằng. So sánh hành vi với kiểu trả về ở phía bên phải và với case đầu tiên của switch bị xóa. Được xem là hoàn tất khi thuộc tính requiredNumber bị thiếu được báo cáo ở dạng ban đầu.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
typescript
Lĩnh vực
compilers
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
48/100

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.