Skip to content

Rule proposal: ban importing .ts files from packages in node_modules when compiled d.ts & .js files are available #2569

Open
@DetachHead

Description

@DetachHead

originally raised at typescript-eslint/typescript-eslint#5815

Description

contrary to popular belief, when publishing npm packages it's actually a good idea to include the source code in the published package. this is because you can use the declarationMap compiler option to make ctrl+click navigate to the source code instead of the declarations.

unfortunately IDEs sometimes incorrectly suggest importing from the source code instead of the compiled code. i think there should be an eslint rule to warn against this

folder structure

my-project/
├── node_modules/
│   └── some-package/
│       ├── dist/
│       │   ├── index.js
│       │   ├── index.d.ts
│       │   ├── foo.js
│       │   └── foo.d.ts
│       ├── src/
│       │   ├── index.ts
│       │   └── foo.ts
│       ├── tsconfig.json
│       └── package.json
└── src/
    └── index.ts

Fail Cases

// my-project/src/index.ts
import {foo} from 'some-package/src/foo'

Pass Cases

// my-project/src/index.ts
import {foo} from 'some-package/dist/foo'

Additional Info

while this won't cause any runtime errors, i believe it's bad for performance as it causes typescript to unnecessarily re-compile the package, when it's already been compiled

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions