-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
int32 type returned by bitwise operators #32188
Comments
Duplicate of #195. |
This is a much more limited proposal. I only care about bitwise operators. I want to use the type system to force developers to coerce to an I wouldn’t advise most developers use this feature. To that end, I’d instead recommend naming the type |
In JavaScript, you can safely represent integers up to 253-1. However, for bitwise operators you only have a range of integers -231 through 231-1. If there were to be an |
I guess the problem here is that people would generally expect It seems like you really just want a brand here? |
It’s actually desirable that the type for function addInt32(x: BitwiseInt32, y: BitwiseInt32): BitwiseInt32 {
return x + y | 0;
} Which has the right properties if you’re looking to do int32 math in JavaScript. |
That’s why I recommend calling the type |
What about a |
Would #33290 help in defining/implementing this? I'd like to see a handful of these types defined. At the very least,
|
I would like #195. I want to use 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 Indeed a workaround is to have a branded 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 #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 |
I'd like to point out the fact that JS "canonically" has these types: // BEGIN BOILERPLATE
interface FixedInt32Array<T extends number> extends Int32Array {
length: T;
}
interface Int32ArrayConstructor {
new<T extends number>(length: T): FixedInt32Array<T>;
}
interface FixedUint32Array<T extends number> extends Uint32Array {
length: T;
}
interface Uint32ArrayConstructor {
new<T extends number>(length: T): FixedUint32Array<T>;
}
type Int32 = FixedInt32Array<1>;
type Uint32 = FixedUint32Array<1>;
// END BOILERPLATE
const
// verbose initialize to 0
n: Uint32 = new Uint32Array(1),
// can't do `Uint32Array([3])`, because of type signature
m: Uint32 = new Uint32Array(1);
// values still mutable, despite `const`
m[0] = 3
const addU32 = (a: Uint32, b: Uint32): Uint32 => {
// can't reuse `a`, because of side-effects
const ret: Uint32 = new Uint32Array(1);
// `a[0] + b[0]` can overflow, so we use `ret` for safety
ret[0] = a[0] + b[0];
return ret;
}
// annoying `[0]`, everywhere
console.log(addU32(n, m)[0]); ( some code borrowed from #18471 (comment) ) By that point, devs would just Number(BigInt.asUintN(32, BigInt(x + y))) |
Search Terms
bitwise operators, integer type, int32
Suggestion
Add an
int32
subtype fornumber
returned by TypeScript from bitwise operators.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 ofnumber
. Only bitwise operations would change to returnint32
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 anint32
parameter to force coercion withn | 0
.Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: