-
Notifications
You must be signed in to change notification settings - Fork 242
TypeScript type adjustments for Socket and Worker #1414
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
Conversation
xs/includes/xs.d.ts
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this means that the concat
method may not be present on some ArrayBuffer
instances. I understand that it is an XS extension, but it is always present when running under XS. My concern is that this declaration will require TypeScript users of XS to write buffer?.concat(...)
. Maybe not the end of the world, but I'd like to the goal of this change better.
I don't recall all the reasons why I needed this, but here is one.
This works perfectly on Node typings/execution, but fails without this change on XS:
I've not done a comparative study between Node and XS, so this may be a naive solution. Perhaps the correct solution is adjusting |
A |
I understand that a view is logically not a buffer, but it does work in Node. Worse, third party modules that I depend upon consume it that way. You can even ask
Will output:
Also,
But on XS it fails to compile:
This
I do agree in hindsight that As always I'm glad to help with a PR, but I need guidance on this one. Thanks! |
The example above generates the same result running under XS as Node.
You can ask ArrayBuffer.isView("123");
ArrayBuffer.isView({});
ArrayBuffer.isView(1);
ArrayBuffer.isView();
That generates the same result in XS and Node too: both agree that an
I think the question here is why does TypeScript let you pass a |
I have
I do think you are correct that Node's typings allow duck typing to work for things like
My thinking about So given all this, if the goal is type compatibility with Node, we might consider something like this:
An approach similar to this would extend the functionality a bit on the core XS buffer services, but hopefully make it type cross-compatible with Node. The devil is in the details, so some experiments would need to be done to be sure the end result works (mock the types and verify compatibility before investing in the implementations). p.s. I mentioned earlier |
I found an ES2024 TypeScript PR. It contains an update to
So.... the problem isn't function doSomething(buffer: ArrayBufferLike): void {}
...
doSomething(myArray.buffer); Note that Changing Your |
Thanks for all your time on this. I really appreciate it. I'll work with the I'll update this PR to remove the |
Thanks for sticking with this. It is a small point, but I don't want to make strange changes to core typings without understanding them well first. The rest of the PR looks reasonable, but for one small detail. The in export interface WorkerOptions {
static?: number;
chunk?: {
initial?: number;
incremental?: number;
};
heap?: {
initial?: number;
incremental?: number;
};
stack?: number;
keys?: {
initial?: number;
incremental?: number;
name?: number;
symbol?: number;
};
} |
Excellent - not sure how I ended up with |
This has been committed and will be available in the next update. |
Three typescript
.d.ts
file changes:socket.d.ts
: define a type forSocket
*xs.d.ts
: Makeconcat
onArrayBuffer
optional as it isn't always available on the variousArrayBufferLike
types.worker.d.ts
: Strengthed the worker types, including adding the creation properties.(Edited to remove the
xs.d.ts
changes, per discussion below)