npm 如何发布 typescript 接口定义 [英] how npm publish typescript interface definition

查看:58
本文介绍了npm 如何发布 typescript 接口定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying to figure this out for the last 3-4 days, googling and reading a lot, but I don't see any example that contains my use case. I want to npm publish a library that contains its types definitions.

我才真正开始做 TS,因为另一个团队需要我的库来支持打字.

I've just really started to do TS because another teams need my library to support typings.

所以让我尝试尽可能多(和尽可能少)的细节:

So let me try to put as much (and as little) detail as I think:

tsconfig.json:

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:

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 中):

src/index.ts (that is build into dist/index.js + dist/index.d.ts):

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

src/types(即内置在 dist/types.js + dist/types.d.ts 中):

src/types (that is build into 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 不会抱怨并构建它们.

I have other code within this repo that use them without issue. tsc doesn't complain and builds them.

现在,我在另一个项目中执行 npm packnpm install ../path/to/my/file-0.0.1.tgz.然后当我想使用我的接口时(在这种情况下,是一个 redux reducer):

For now, I do npm pack and npm install ../path/to/my/file-0.0.1.tgz in my other project. Then when I want to use my interfaces (in this case, a 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.

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

I really cannot figure out why this is happening. Is there another step I need to do in order to build my definition file? Ideally, I would prefer not to have to build it by hand.

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

Let me know if I need to provide more details.

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

Thanks for your help and patience reading this.

推荐答案

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

You need to include something like the following in your package.json

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

这篇关于npm 如何发布 typescript 接口定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆