You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何禁止直接导入TypeScript库内部符号并隐藏源文件路径?

How to Restrict Direct Imports of Internal Symbols in Your TypeScript Package

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.

Double-check your tsconfig.json to ensure:

  • declaration is set to true (to generate proper type definitions)
  • declarationDir points to dist
  • If you use declarationMap, consider disabling it temporarily—while the @internal comment 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 18:12:43