microsoft / microsoft/TypeScript

noImplicitReturnType [Suggestion]

Đang mở
#15,733 8 bình luận 6 reaction 0 người được giao Xem trên GitHub

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

In Discussion Suggestion
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ả

We have noImplicitReturns which is great and helps tick up silly mistakes but it woud great to have noImplicitReturnType as well.

So what would noImplicitReturnType do?
Currently we can write a function like so:

function life () {
    return 42;
}
let x = life(); // Number

So all is great, TypeScript works out the return type nicely for us.

function life (x: boolean) {
    if (x) {
        return 'hi';
    } else {
        return 42;
    }
}
let y = life(true); // Number / String

This is where things start to go a little crazy. TypeScript records y as of type number | string which is of course acurate. But what if i wanted this function to only return strings?

We already have support for that with limited modifications

function life (x: boolean): string {
    if (x) {
        return 'hi';
    } else {
        return 42; // Type '42' is not assignable to type 'string'.
    }
}
let y = life(true);

And as a result TypeScript nicly throws an error.

So my suggestion is that in much the same way as with noImplicitAny and let enforcing strict typeing an argument is added in the form of noImplicitReturnType which for my first example (only a single return) wouldn't have any effect but for the second would throw an error instead of typing the function as number | string

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 bằng cách so sánh các tùy chọn noImplicitReturns và noImplicitAny hiện có với hành vi noImplicitReturnType được đề xuất, như mô tả trong các ví dụ. Xác định các chẩn đoán dự kiến cho những hàm có kiểu trả về được suy luận là một union, và định nghĩa thế nào được xem là hoàn tất đối với tùy chọn trình biên dịch mới; issue không nêu tệp hay test nào.

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
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
30/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.