Fixing absolute imports in TypeScript

Cute Feature: absolute imports with baseUrl

"compilerOptions": {
"baseUrl": "src"

So now your module can’t be imported into other modules

  • Compile your TypeScript with @babel/typescript instead. Drawback: No .d.ts definition files, and no type safety at compile time.
  • In a TypeScript parent project, set up your parent (importing) project to resolve imports relative to node_modules/myModule/lib using tsconfig-paths/register, or manually “absolute to project-root relative” mappings in your parent’s tsconfig.json. Very manual.
  • Build your library with rollup-plugin-typescript2, which produces a single file with no imports, but no typing definition files.
  • Build using webpack and awesome-typescript-loader, with the right plugin configuration.
  • And others tsc hacks involving symlinks or post-build scripts

Best solution: Use the compiler itself to compile these imports to Javascript




i make cool shit on the internet

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tom McKenzie

Tom McKenzie

i make cool shit on the internet

More from Medium

Roman to Integer in Typescript (again :))

No-Op Render Logic for React Components: Some Options

Typescript explained by a dummy

Typescript with Grafikart, my resume