microsoft / microsoft/TypeScript

Officially document type checking behavior for operators WRT bigint

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

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

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

Search Terms

bigint operator type checking

Suggestion

I'd like to see official documentation of the design decision TS has made regarding type checking of operators now that bigint
is in play.

Use Cases

The closure-compiler team is currently working on our support for BigInt.
We want to make sure the type design choices we make there are compatible with TS going forward.

It appears that right now when you perform an arithmetic operation on 2 any values, TS infers the result to be of type number, even in ESNext mode where one might expect the result to be number|bigint.

https://www.typescriptlang.org/play/?target=99#code/DYUwLgBAhgXNB2BPCBeCBGeBuAUKSARnFEqhtnuBAMZwECWA5vfJGlBAFQQFZA

I am inclined to do the same for closure-compiler, but I'd first like to make sure that this behavior was an intentional design choice for TS and not an oversight/bug that is likely to change in the near future.

My point of view on this is that logically the * operator (for example) is overloaded, so there are really 2 versions of it.

  1. The original * can take anything (except bigint), and always returns a number.
  2. The new bigint * bigint version, which always returns a bigint, or throws an exception if one operand is not a bigint.

The compiler should assume it's dealing with #1, unless type information indicates it is seeing #2.

Insisting that all operands be clearly typed as bigint if you want bigint results prevents users from
crossing the streams and accidentally mixing bigint and number values in computations.

Examples

  1. What is the result type of bigint|number * bigint|number? I expect bigint|number with no error message, since you may want to write methods that are intentionally meant to work for either case.
  2. What is the result type of bigint * any? I expect the bottom/can't happen type, and an error message since we can't be sure that the right operand will be a bigint at runtime.
  3. What is the result type of any * any? I expect number (current TS behavior), because it's undesirable for old code to suddenly think it might have a bigint, and new code should be explicit about using bigint to avoid runtime errors.

Checklist

My suggestion meets these guidelines:

  • [ X ] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [ X ] This wouldn't change the runtime behavior of existing JavaScript code
  • [ X ] This could be implemented without emitting different JS based on the types of the expressions
  • [ X ] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [ X ] This feature would agree with the rest of TypeScript's Design Goals.

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à các ví dụ về toán tử trong issue. Xác nhận hành vi dự kiến đối với bigint, number và mọi quy tắc dựa trên hành vi kiểm tra kiểu hiện tại, sau đó ghi lại quyết định và lý do trong tài liệu phù hợp của dự án. Hoàn tất khi hành vi của toán tử và các trường hợp tiêu biểu được nêu chính thức.

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, documentation
Loại issue
Tài liệu
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
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
38/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.