如何禁止直接导入TypeScript库内部符号并隐藏源文件路径?
Got it, let's solve this problem properly. The core issue is that TypeScript and VS Code are exposing your internal exported symbols even though you only want users to access them via the ORM namespace. Here's a step-by-step fix:
1. Mark Internal Symbols as Internal
First, in the file where ColumnsDecoratorFactory is defined (like src/modules/orm/decorators/column.ts), add a /** @internal */ JSDoc comment above its export. This tells TypeScript the symbol is for internal use only, so VS Code will stop suggesting it as an import option:
/** @internal */ export const ColumnsDecoratorFactory = /* your decorator implementation */;
2. Avoid Direct Exports in Internal Module Entries
Make sure your src/modules/orm/index.ts only exports the ORM object, not individual symbols like ColumnsDecoratorFactory. For example:
import { ColumnsDecoratorFactory } from './decorators/column'; // Only export the ORM namespace object, not the decorator directly export const ORM = { ColumnsDecoratorFactory };
This way, your root src/index.ts (which imports and re-exports ORM) won't carry over any direct exports of internal symbols.
3. Lock Down Package Imports with package.json exports
This is the most critical step to block direct imports of internal dist paths. Add an exports field to your package.json that explicitly allows only the root import, and blocks all other subpaths:
{ "main": "dist/index.js", "types": "dist/index.d.ts", "exports": { ".": { "require": "./dist/index.js", "import": "./dist/index.js", "types": "./dist/index.d.ts" }, // Block all other subpath imports "./dist/*": null, "./modules/*": null, "./src/*": null } }
The null value tells Node.js and module resolvers those paths are inaccessible, so VS Code will stop showing them as valid import options.
4. Tweak TypeScript Config (Optional but Recommended)
Double-check your tsconfig.json to ensure:
declarationis set totrue(to generate proper type definitions)declarationDirpoints todist- If you use
declarationMap, consider disabling it temporarily—while the@internalcomment should still work, disabling the map can eliminate any edge-case path suggestions.
After rebuilding your package, users will only be able to access ColumnsDecoratorFactory via import { ORM } from '@ts-stack/mod'; ORM.ColumnsDecoratorFactory, and VS Code won't show any direct import options for the internal symbol.
内容的提问来源于stack exchange,提问作者ktretyak

