microsoft / microsoft/TypeScript
Suggestion: noInferredAny
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.4k
- Merge trung bình
- 1 ngày 19 giờ
- Pull request đã merge (30 ngày)
- 117
Mô tả
Search Terms
noImplicitAny inference any noInferredAny
Suggestion
Symbols typed as any reduce the type safety of a program. That's fair if the programmer intended to use any. However it's relatively easy to end up with symbols typed any through type inference chains that are not obvious:
- 3rd party libraries might return
any - complex type expressions might infer to any
- some
.d.tstypes default generic arguments toany(e.g.Set)
In all these situations, a programmer might write const foo = something(); and expect foo to have a reasonable inferred type. foo being inferred to any is easy to miss in such code, both while editing and while reviewing code.
Proposal: add a compiler option noInferredAny that flags symbols whose type is any and that do not have an explicit type annotation.
Use Cases
- better type safety for programs
- detect weak typings in your dependencies
- more
Examples
const foo1 = returnsAny(); // error
const foo2: any = returnsAny(); // ok
const foo3: string = returnsAny(); // probably ok, programmer gave an explicit type? could also require an explicit cast.
const foo4 = returnsAny() as string; // ok
const {foo5}: {foo5: string} = returnsAny(); // ok
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với đề xuất và các ví dụ của đề xuất, đặc biệt là sự phân biệt giữa any được suy luận và any được chú thích rõ ràng. Xem xét các trường hợp suy luận đã liệt kê và xác định tùy chọn phải hoạt động như thế nào đối với từng trường hợp; hoàn tất nghĩa là trình biên dịch có thể nhất quán cảnh báo các ký hiệu không được chú thích nhưng được suy luận là any mà không thay đổi JavaScript được phát ra.
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
- 25/100