Replies: 1 comment 1 reply
-
|
Thank you for writing this up! I think we can just remove it. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I don't know what category this belongs in, it's a mix of Idea and Q&A.
Bun implemented the
Array.fromAsyncproposal a while ago.In June 2024, I filed an issue about it being mistyped (#8484) and a PR to fix it (#8486).
Since then, TypeScript has documented the
ArrayConstructor.fromAsynctypes in thelib.esnext.array.d.tstype declaration.These declarations differ, which should not happen for a language type. (only platform defined globals, such as
fetch, should have different types/behaviours)Here's the type I submitted in 2024:
And now the implementation in TypeScript's codebase:
The doc comments from TypeScript are a lot more terse and lack examples, but that's a matter of taste; and being more terse makes sense as it slowly gains attraction and needs fewer explanations (plus, there's always MDN for that).
The real problem is that the types are different.
In Bun's type declaration, the
arrayLikecannot be an array that contains bothTandPromise<T>. Which means that for the exampleArray.fromAsync([1 as const, Promise.resolve(1 as const)] as const)hovering overfromAsyncwould showArray.fromAsync<1 | Promise<1>>whild the TypeScript definition would only showArray.fromAsync<1>. It is a minor difference, but still should be considered in my opinion.Also, when a map function is provided, it has a different behaviour.
I followed the MDN documentation here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fromAsync#using_mapfn_with_an_async_iterable
The example and explicitelly tells
TypeScript's declaration does not respect this and awaits the parameter of the map function, meaning the above example is incompatible with the type declaration.
However, after some testing, I found something weird. Here's the official spec from ecma262 https://tc39.es/ecma262/#sec-array.fromasync, which proves MDN's documentation right.
But testing on Chrome, Firefox, Node and Bun shows that none of them follow the spec! (from my understanding, I may be wrong; also I don't have any way to test on Safari currently)
Which makes me question the whole method and its implementation in Bun.
Should we stick to the current types with the "broken" implementation?
Should we fix the implementation to follow the spec and keep our current types?
Should we delete the type declaration to default to TypeScript's one and align with other implementations?
Maybe I have misunderstood something here in the spec and MDN documentation, in which case I'd love an explanation please. Otherwise, if really no major engine correctly implements this method from the spec, we probably should think about it.
If there were no behaviour differences I would propose deleting the type as it clearly belongs in TypeScript's lib in my opinion.
Beta Was this translation helpful? Give feedback.
All reactions