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
TypeError: Cannot read properties of undefined (reading 'Symbol(Body internals)') #1647
Comments
We are affected by this too in a simple Using node-fetch because without it typescript with ts-fp complains about lack of types. |
We should not do anything to fix this. cuz this can't / shouldn't work even if we could solve this issue and allow for
No they aren't. You will lose the context of what the function belongs to. an example of this could be class C {
f() {
console.log('ctx is:', this)
}
}
new C().f() // > ctx is: C { }
var {f} = new C(); f() // > ctx is: undefined We arn't doing any brand checking/validation (as we should be doing anyway - but haven't gone around at fixing it) The actual "Expected behavior" would would rather be an error with: There are many other things this aplies to. It's basically on all kind of methods off any es6 class. heck even test this out in browser too see if it also works: (hint it dose not work) var {text} = new Response('x')
text() // gives a TypeError Illegal invocation (as expected) if you want this kind of destructoring to work, then you would have to use bind or call var response = new Response('x')
var { text } = response
text = text.bind(response); text() // works
await text.call(response) // or do this... |
closing this as the "won't fix" if anything there should be an issue of "Do type brand checking and throw TypeError if it's decoupled of its own class that it belongs to" kind of issue Bu doing something like text() {
if (!(this instanceof Response)) {
throw new TypeError(...)
}
} |
Sometimes i think developer over abuse destructoring too much for literal everything and making stuff less readable... Even the minified destructoring version will be larger sometimes. window.onmessage = ({data}) => console.log(data) // not worth it...
window.onmessage = e => console.log(e.data) // this is shorter You will also get lost in the "of what context is this xyz doing?" kind of senarios. (you loose a namespace) // Somewhere at the top of the file:
const {encode} = new TextEncoder()
const {encode} = require('to-hex')
const {join} = require('node:path')
// later way down at the bottom of your script you do:
join(...xyz) // ...and you ask yourself where dose join come from? is it a array join method? It makes it more readable / understandable if you had a tiny const textDecoder = new TextDecoder()
const toHex = require('to-hex')
const fs = require('node:path')
fs.join('a', 'b')
textDecoder.decode(x)
toHex(xyz) Destructoring should mostly only be done
// There is no need to destruct fs or path as the hole `fs` & path will be GC'ed afterwards
// Unless you want to ship a browserified web bundle and do tree shaking
import fs from 'node:fs'
import path from 'node:path'
const file = path.join(import.meta.url, '../readme.md')
const pkg = fs.readFileSync(file)
export default pkg // make since to destructor fs cuz you only need one thing and you are reusing it
import { readFileSync } from 'node:fs'
export function readJson(path) {
return readFileSync(path)
} |
Thanks for the comprehensive response @jimmywarting - I learned a lot. Of course losing the binding context makes sense here, and the error as presented did't immediately make sense, but after reading your notes, it's perfectly understandable. Thanks also for the reading suggestions and thorough explanation through code snippets. |
Reproduction
This works
while the syntatically equivalent
fails with
TypeError: Cannot read properties of undefined (reading 'Symbol(Body internals)')
.Expected behavior
Both code blocks should work as they are syntactically equivalent. It might be related to NodeJS primordials, see NodeJS Issue #36364.
Your Environment
The text was updated successfully, but these errors were encountered: