eremzeit eremzeit - 4 months ago 14x
Javascript Question

When using ES6 import statment, is there a way to protect against items being undefined?

import {
foobor3, //typo! this key doesn't exist in the module.

} from './module_file.js'

console.log(foobar1, foobar2, foobar3) //EXPLODES

One of the most frequent silly mistakes I make when using the new ES6 style import statement is that I'll have a typo in one of the keys in object destructuring. I can't think of a single instance where I'd ever want a value in a destructuring assignment to be
. Is there any way to force the import statement to fail-fast if one of the items I'm trying to import is undefined?


import {
doesntExistInModule //EXPLODE NOW!


The module stuff in the spec is pretty gnarly, but I believe a real implementation will throw a SyntaxError at ModuleDeclarationInstantiation( ) Concrete Method step 12.d.iii in that case. Since there are no legit implementations I don't know if you're talking about a way to do it in transpiled code in the meantime, or if you don't realize that's the case and will be satisfied to know it'll work that way eventually. There's been talk before of trying to implement that kind of check in Babel, but as far as I know nothing's actually been done to that effect. Babel compiles each module in isolation.

Also, this is not object destructuring, it just has similar syntax.