TypeScript: tsconfig paths break when consuming the compiled declaration files from a different package

TypeScript Version: tried 2.2.1 and 2.5.2

Code

src/file1.ts

export type myType = {anything : number};

src/file2.ts

import { myType } from '@src/file1';

export type myOtherType = {anythingElse : number} & myType;

tsconfig.json

{
  "compilerOptions": {
    "outDir": "./dist",
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "declaration": true,
    "noImplicitAny": true,
    "removeComments": true,
    "sourceMap": true,
    "strictNullChecks": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "noUnusedParameters": true,
    "noUnusedLocals": true,
    "baseUrl": ".",
    "paths": {
      "@src/*": ["./src/*"]
    },
    "lib": ["es2017", "dom"]
  },
  "compileOnSave": false,
  "include": [
    "src/**/*",
    "app/**/*"
  ],
  "exclude": [
    "node_modules",
    "dist"
  ]
}

Expected behavior: i should be able to import myOtherType in a different package (after say publishing this to npm, with the typings field set to dist/file2.d.ts) without errors

Actual behavior: typescript says Cannot find module "@src/file1" when compiling any package that consumes these types.

Note: the runtime behavior works properly in my case because i’m handling the module resolution through webpack. It’s only the types that are failing and I’m curious how the paths property could be expected to work outside of the package in which they are defined. Is that property simply not meant to be used in a library style package that’s being written for external consumption?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

@mhegazy I understand the reasoning behind, but from a developer perspective, this is really not convenient.

We have had a few issues around that, but nothing clearly asking for a rewrite of module names. feel free to create a new suggestion for it.

the official recommendation for importing local modules is to always use relative paths if the module is going to be published? and just suss out how many …/s you need?

yes. that would be my recommendation.

I am not familiar with the webpack resources you mention,

To fill in the gap: Webpack has the resolve field to say “an import of this form should be resolved specially to XYZ”. TypeScript’s path mapping functionality is available so users can model that type of behavior specifically.