int32 type returned by bitwise operators
See original GitHub issueSearch Terms
bitwise operators, integer type, int32
Suggestion
Add an int32
subtype for number
returned by TypeScript from bitwise operators.
function coerce(n: number): int32 {
return n | 0;
}
JavaScript bitwise operators coerce arguments to int32 and the return type of those operators will always be an int32.
This would not be a breaking change since int32
would be a subtype of number
. Only bitwise operations would change to return int32
s.
EDIT: I now recommend calling the type BitwiseInt32
so that user’s won’t see the type as a generic integer type.
Use Cases
Helps applications which are trying to take advantage of JavaScript implementations int32
optimizations. An application could ask for an int32
parameter to force coercion with n | 0
.
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.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:10 (3 by maintainers)
Top Results From Across the Web
What is the datatype returned by the bitwise complement ...
The result of the ~ operator is the bitwise complement of its (promoted) operand (that is, each bit in the result is set...
Read more >Bitwise Operators in C/C++
In C, the following 6 operators are bitwise operators (work at ... and !) is either 0 or 1, but bitwise operators return...
Read more >Bitwise AND (&) - JavaScript - MDN Web Docs
The bitwise AND (&) operator returns a 1 in each bit position for which the corresponding bits of both operands are 1s.
Read more >Bitwise Operators - L3HarrisGeospatial.com
For integer, longword, and byte operands, NOT returns the complement of each bit of the operand. For floating-point operands, the result is 1.0...
Read more >C++ Bitwise Operators
Note: Bitwise operators can only be used alongside char and int data types. ; The bitwise AND & operator returns 1 if and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
What about a
uint32
type for>>> 0
?I would like #195. I want to use
int32
for performance reasons: I have one use case where numbers up to2**31-1
is enough (array indexes, I know these can go up toNumber.MAX_SAFE_INTEGER
but I do not care about handling numbers that large), and where coercing parameters and the output of every arithmetic operation by adding| 0
(for instance,++i
~(i = i+1|0)
) yields significant time saving and produces compiled code almost twice smaller even though more subroutine calls are inlined (10KB instead of 17KB, I used Indicium to witness that).I think what I have experienced can be extrapolated. I have the feeling that this addition would make basic algorithms in JavaScript much less resource consuming (if you agree to limit yourself to inputs of reasonable size). This claim would need to be evaluated.
I agree this example use case is a niche. I do not want
int32
to be the newnumber
. Even more so if it means errors due to implicit integer division or integer overflow.Indeed a workaround is to have a branded
int32
type, a coercion function, and arithmetic functions. But I have sufficiently many arithmetic operations in my code that I fancy leaving the responsibility to TypeScript would not be a luxury. I am probably wrong. Maybe all we need is a tiny library which includes said type and functions.Nervetheless, if this responsibility would be shifted to TypeScript, I can see two problems in code emission:
PS: If going the workaround route then explicit inlining hints would be relevant (see https://github.com/microsoft/TypeScript/issues/661 for instance). The current v8 is excellent at inlining what needs to be inlined, I do not deny it, I have seen it in action and it is impressive. That does not contradict wanting more control on code emission. The better argument would be that implementing this takes time both for conception and maintenance, and has limited applicability. If we want to argue code size, replacing
a(x,y)
byx+y|0
uses one less character buta(a(x,y),z)
would need parentheses(x+y|0)+z|0
and now we are equal, inlininga(a(a(x,y),z),w)
emits something with one more character. Not sure if output size is relevant. The case(i = i+1|0)
would be better served by a macro or a code transform as you cannot wrap the assignment in a function call but if we cannot have that then(i = f(i))
already saves one character because the right operand is constant. Hence we cannot argue inlining will always save bytes, nor can we argue it will always use more. A reasonable argument against implementing inlining hints in TypeScript is that minifiers can do that job, giving even more control (although, at the moment, terser produces an IIFE for some (each?) inlined call, and there is no way to guide the process).