Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

import {
  foobar1,
  foobar2,
  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 undefined. Is there any way to force the import statement to fail-fast if one of the items I'm trying to import is undefined?

ie:

 import {
  doesntExistInModule  //EXPLODE NOW!  πŸ”₯πŸ”₯πŸ”₯
} from './module_file.js'
like image 715
eremzeit Avatar asked Apr 23 '16 01:04

eremzeit


1 Answers

  • There is no hook allowing a module to run some code before its dependencies load. This means that modules have no control over how
    their dependencies are loaded.
  • There is no error recovery for import errors. An app may have hundreds of modules in it, and if anything fails to load or link, nothing runs. You can’t import in a try/catch block. (The upside here is that because the system is so static, webpack can detect those errors for you at compile time.)

For more details, read it out

like image 77
coder Avatar answered Nov 15 '22 14:11

coder