-
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
Type error in Buffer.from() #23155
Comments
Overload resolution is not distributed over arguments of union type. See #14107 for more details. For this specific issue we can take a targeted fix to change the overloads of from(buffer: any[] | ArrayBuffer | string | Buffer): Buffer;
from(arrayBuffer: ArrayBuffer, byteOffset: number, length?: number): Buffer;
from(str: string, encoding: string): Buffer; |
So to be clear, we are accepting a PR for updating the signature of |
@mhegazy I think this issue is not related to |
Doh! thanks @a-tarasyuk for the correction. yes this should be done on the DT side. and looks like that you fixed it already in DefinitelyTyped/DefinitelyTyped#24966. thanks! |
Sorry to be late to the game; I'm not a fan of the fix:
Do we really need to muddy the codebase when the workaround is so simple? It seems like the right thing to do is ask someone to "me too" the original issue and add the 3LOC workaround: function doStuff(foo: string|Buffer) {
// https://github.com/Microsoft/TypeScript/issues/14107
if (typeof foo === 'string') {
// Now my code can even handle strings that were in hex64,
// not ascii.
foo = Buffer.from(foo/*, optEncoding */);
}
// do real stuff
} |
It isn't like this is a big deal for us or anything, just found it to be a minor inconvenience and something I should probably report :) I'm perfectly happy to wait for #14107 to land. |
Sorry, I didn't mean to bite off anyone's head either. I'm really glad you reported the issue and I hope that the bug leaves a paper trail so more people can add pressure to #14107; the TypeScript team has got loads on their plate and +1s help them balance priorities. @a-tarasyuk; would it be possible to revert since the fix added bugs? @JustinBeckwith should be able to work around with the |
Yeah. If @mhegazy is onboard
…On Mon, Apr 16, 2018 at 12:40 AM Alexander T. ***@***.***> wrote:
@inlined <https://github.com/inlined> sure, I can make PR. The fix is
ready just need to get approve from @mhegazy <https://github.com/mhegazy>,
and make PR on GitHub. Does it work for you?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#23155 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABe1j0ysf3PlWm1EhgBpipGxXKwLh8vEks5tpEsAgaJpZM4THpfV>
.
|
I do not think the change is bad really. The order of the overloads can be fixed to address the issues you are referring to earlier. But in general it is better to use union types than having multiple overloads. |
@inlined It's also available in NodeJS 4 - see https://nodejs.org/dist/latest-v4.x/docs/api/buffer.html#buffer_class_method_buffer_from_str_encoding |
@inlined I expect you refer to By the way, which exact use is the problem now? I locally added |
Looks like I've overreacted to everything. I retract my previous comments. Thanks everyone for being patient with me here.
Is it not reasonable to expose the type in
Yup... A total facepalm moment on my part. I missed the one overload after the GitHub diff collapse.
Interesting; the v4 docs and 4.5 release notes agree. The latest docs misleadingly say "added in 5.10". Should we not be trusting those docs when reviewing this codebase? |
I found that my last statement in this is outdated - nodejs typings refer to lib.es6.d.ts already so There are a few other locations where
Well, I did a fast try and then it was clear the v4 doc is correct. I expect this is a side effect of how NodeJs backports new APIs. First they land on master and get released (e.g. in 5.10). The docs on master tell exactly this. A few weeks later the backport is done (for selected features only) by merging the relevant changes to v4 branch - but docs on master are not updated automatically. |
Greetings folks! 👋
I'm getting a type error I can't explain. I have some code like this:
This gives me the compilation error:
This is weird, because
Buffer.from
can accept a string or a Buffer. The d.ts for node.js appears to check out:This kinda feels like a bug. Am I missing something? Thanks!
cc @ofrobots
The text was updated successfully, but these errors were encountered: