Repository navigation
JSON parse works with type Buffer #44944
Description
Activity
Same as #17203 and moreover
Bufferis Node specific type and doesn't looks like it belongs to standard lib files.Reacted by Martin Johns and Kristian Rykkje- addedWorking as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
on Jul 8, 2021 RyanCavanaugh commented
on Jul 8, 2021 MemberMore actionsThis is just an implicit coercion, not anything
JSON.parsedoes.Reacted by Kristian Rykkje and Martin JohnsThis is just an implicit coercion, not anything JSON.parse does.
Literally false, see the spec step 1:
Let jsonString be ? ToString(text)which explicitly convertstextarg to it's string representation before doing anything elseRyanCavanaugh commented
on Dec 2, 2023 MemberMore actionsI'm curious what you think an implicit coercion is?
Everything you said is true.
But I still think the original comment has some point. It should not be a TS error to pass a Buffer to
JSON.parse()since it’s pretty valid. Yes it’s an implicit coercion but it does make sense to use it there. Though I don’t know how you could patch this properly. It should still work in an ideal world.RyanCavanaugh commented
on Sep 11, 2024 MemberMore actionsIf you have a raw
Bufferyou don't know if it's UTF-8, UTF-16, base64, etc.. Arguably the entire point of TS is to tell you when you're making assumptions like that.const b = Buffer.from("{}", "utf-16le"); JSON.parse(b); // throws
Reacted by Andrii Dieiev- locked as resolved and limited conversation to collaborators
on Oct 21, 2025
lib Update Request
Configuration Check
My compilation target is
ES2015and my lib isthe default.Missing / Incorrect Definition
parse(text: string, reviver?: (this: any, key: string, value: any) => any): any;Sample Code
const object = JSON.parse(readFileSync("sample.json"));Documentation Link
https://262.ecma-international.org/5.1/#sec-15.12.2
https://github.com/nodejs/node/blob/c41c09375b217d9daec614edb590147b14ff9b21/lib/buffer.js#L405
Suggestion
parse(text: string | Buffer, reviver?: (this: any, key: string, value: any) => any): any;