microsoft / microsoft/TypeScript
Officially document type checking behavior for operators WRT bigint
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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.
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.
- The original
*can take anything (exceptbigint), and always returns anumber. - The new
bigint * bigintversion, which always returns abigint, or throws an exception if one operand is not abigint.
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
- What is the result type of
bigint|number * bigint|number? I expectbigint|numberwith no error message, since you may want to write methods that are intentionally meant to work for either case. - What is the result type of
bigint * any? I expect thebottom/can't happentype, and an error message since we can't be sure that the right operand will be abigintat runtime. - What is the result type of
any * any? I expectnumber(current TS behavior), because it's undesirable for old code to suddenly think it might have abigint, and new code should be explicit about usingbigintto 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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem verlinkten TypeScript-Playground und den Operatorbeispielen im Issue. Bestätige das beabsichtigte Verhalten für bigint, number und alle Regeln anhand des aktuellen Type-Checking-Verhaltens und dokumentiere anschließend die Entscheidung und Begründung in der dafür vorgesehenen Dokumentation des Projekts. Als erledigt gilt die Aufgabe, wenn das Operatorverhalten und repräsentative Fälle offiziell festgehalten sind.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers, documentation
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 38/100