Skip to content

Instantly share code, notes, and snippets.

@alexilyaev
Last active February 26, 2023 18:23
Show Gist options
  • Save alexilyaev/a0ea5f8990d721ff78cb4fec7acfe780 to your computer and use it in GitHub Desktop.
Save alexilyaev/a0ea5f8990d721ff78cb4fec7acfe780 to your computer and use it in GitHub Desktop.
Why not `index` files?

Why not index files

Using index.{jsx,tsx} for Components or index.{js,ts} for other files

  • Hard to search for a component or file based on it’s name (the index file is only on the 7th result)

image

  • Opening several index files in the IDE results in hard to read tabs with needless index keywords

image

What about using index files only to re-export other files?

  • Requires more discipline or linting to enforce proper convention
  • It’s easy to fall for an indirection pattern where you import some stuff from other files and export them in different names
    • Which makes it harder to navigate the code with Jump to Definition or Find References
  • Confusing to choose between importing from the index or explicitly from the target file
    • Sometimes we don’t need everything from the index but only 1 thing
    • Importing needless stuff can bloat up the bundle size
  • Other good reasons against it:

What about TypeScript?

TypeScript does support it out of the box, but we have to set moduleResolution: "node" if we set module to es2015 or above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment