typescript - npm 如何发布 typescript 接口(interface)定义

在过去的 3-4 天里,我一直在尝试解决这个问题,通过谷歌搜索和大量阅读,但我没有看到任何包含我的用例的示例。我想npm 发布 一个包含其类型定义的库。

我才真正开始做 TS,因为其他团队需要我的库来支持打字。

所以让我尽可能多(和尽可能少)地添加我认为的细节:

tsconfig.json:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "allowUmdGlobalAccess": true,
    "baseUrl": ".",
    "declaration": true,
    "declarationMap": true,
    "forceConsistentCasingInFileNames": true,
    "jsx": "react",
    "module": "commonjs",
    "noImplicitAny": true,
    "noUnusedParameters": true,
    "noUnusedLocals": true,
    "outDir": "./dist/",
    "paths": {
    },
    "sourceMap": true,
    "strict": true,
    "target": "es6",
  },
  "include": [
    "./src"
  ]
}

package.json:

  "main": "dist/index.js",
  "scripts": {
    "tsc": "tsc",
    "prepack": "npm run clean && npm run tsc",
  },
  "types": "./dist/index.d.ts",

src/index.ts(内置于 dist/index.js + dist/index.d.ts):

export { IAction, IState } from './types';

src/types(内置于 dist/types.js + dist/types.d.ts):

import { Map } from 'immutable';

export interface IAction {
  type: string;
  payload: object;
}

export interface IState extends Map<string, Map<string, any>> {
}

我在这个 repo 中有其他代码可以毫无问题地使用它们。 tsc 不会提示并构建它们。

目前,我在我的其他项目中执行 npm packnpm install ../path/to/my/file-0.0.1.tgz。然后当我想使用我的接口(interface)时(在本例中,是一个 redux reducer):

import { IAction, IState } from 'my-lib';  // <-- match my package.json name

const reducer = (state: IState, action: IAction) => {
  ...
}

我收到以下错误:

error TS2709: Cannot use namespace 'IState' as a type.
error TS2709: Cannot use namespace 'IAction' as a type.

我真的想不通为什么会这样。为了构建我的定义文件,我还需要执行其他步骤吗?理想情况下,我宁愿不必手动构建它。

如果我需要提供更多详细信息,请告诉我。

感谢您的帮助和耐心阅读。

最佳答案

您需要在 package.json 中包含如下内容

  ...
  "main": "dist/index.js",
  "typings": "dist/index.d.ts",
  "source": "lib/index.ts",
  "files": [
    "dist",
    "lib"
  ],
  ...

https://stackoverflow.com/questions/59742688/

相关文章:

python - PyAutoGUI,停止命令

excel - 查找工作表中的总行数和列数 - Microsoft Graph

javascript - CSS 不透明度过渡不适用于 Safari

c++ - 如何检测非事件 Win32 应用程序中的最小化?

vagrant 2.2.7 - 无法 vagrant up : default: Warning:

c# - 在 Blazor 应用程序中使用 Azure Active Directory 时如何在

git - 无法将 'core.repositoryformatversion' 设置为 '0'

reactjs - 类型 'background' 上不存在属性 '{}' - React Rout

java - Slf4j,logback - 从 json 中删除 mdc 标签

android - 尝试在使用谷歌驱动器的 android 应用程序(使用 kotlin)webvi