Skip to content

Fix #61429: Fast arithmetic type check for type-parameters upper-bounded by bigint fixed #61571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

CurtisFenner
Copy link

@CurtisFenner CurtisFenner commented Apr 13, 2025

Fixes #61429

The maybeTypeOfKind function is a simpler version of full type-checking which is used by arithmetic. Previously it was used to determine whether arithmetic operations such as +, <<, and ~ were acting on number types or bigint types.

Note that these type checks are somewhat complicated by a desire to preserve unsoundness: any * any should be number, rather than the (sound but exceedingly impractical) number | bigint.

Previously, the maybeTypeOfKind function did not return true when an operand has a type T, regardless of the bounds of T. This meant that when maybeTypeOfKind is passed T extends bigint and bigint, it returned false.

This caused expressions like a & b to be assigned the "default" type number instead of bigint.


To fix this issue, I updated the checks to instead use maybeTypeOfKindConsideringBaseConstraint. I suspect that some of the other uses of maybeTypeOfKind have similar issues, but if so they seem much more subtle and I wasn't able to construct any suspicious tests for them.


I added the bigintSubtypingTypeParameter test which includes a variety of unary and binary operators acting on such T extends bigint values.


This is my first PR, so apologies if I'm missing something important.

@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Apr 13, 2025
@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Apr 13, 2025
@CurtisFenner
Copy link
Author

@microsoft-github-policy-service agree

@CurtisFenner CurtisFenner force-pushed the fix-61429-bigint-type-parameter-maybeTypeOfKind-bug branch from ae470a1 to 1390f5f Compare April 18, 2025 00:44
@CurtisFenner CurtisFenner force-pushed the fix-61429-bigint-type-parameter-maybeTypeOfKind-bug branch from 1390f5f to 71d535a Compare April 18, 2025 00:47
@CurtisFenner
Copy link
Author

CurtisFenner commented Apr 27, 2025

@jakebailey Hi, I'm not sure if you missed the new approach I took. I found a more appropriate maybeTypeOfKindConsideringBaseConstraint and audited all the existing callers of maybeTypeOfKind to see which obviously should be using it. (I also did an extraction of getBinaryArithmeticResultType into a function since it was a large code block in a large chain of alternatives, to be more similar to the getUnaryResultType)

Apologies if you were already aware, I'm sure you have more important tasks to attend to for TypeScript!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Status: Not started
Development

Successfully merging this pull request may close these issues.

Arithmetic operations on type-parameters subtyping bigint are incorrectly inferred to have type number
3 participants