打字稿没有找到类型定义

时间:2017-08-08 17:43:25

标签: typescript npm

我制作了一个我在npm发布的打字稿模块,但是我无法让intellisens工作。

文件结构就是这样

dist
  index.js
  + others
src
  index.ts 
  + others
package.json
tsconfig.json

src包含.ts个文件,与.js.d.ts.js.map分开。

如果我这样做:

import { X } from 'my-package';

它会起作用,没有错误,但没有智能,消息是the type definition is not found

但是,如果我这样做:

import { X } from 'my-package/dist';

我确实得到了智慧。

在package.json中,我把:

   "main": "dist/index.js",

这是我的tsconfig:

   {
  "compilerOptions": {
    /* Basic Options */
    "target": "es6",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "declaration": true,                   /* Generates corresponding '.d.ts' file. */
    "sourceMap": true,                     /* Generates corresponding '.map' file. */
     "outDir": "./dist",                        /* Redirect output structure to the directory. */
     "rootDir": "./src",                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    "removeComments": false,                /* Do not emit comments to output. */

    "strict": true,                            /* Enable all strict type-checking options. */

     "sourceRoot": "./dist",                    /* Specify the location where debugger should locate TypeScript files instead of source locations. */
     "mapRoot": "./dist"                       /* Specify the location where debugger should locate map files instead of generated locations. */
    },"exclude": [
    "test/**"
  ]
}

1 个答案:

答案 0 :(得分:2)

您需要单独引用.d.ts文件。请参阅TypeScript手册中的publishing部分。

package.json

{
    "name": "awesome",
    "author": "me",
    "version": "1.0.0",
    "main": "./dist/index.js",
    "types": "./dist/index.d.ts"
}

请注意types键,其值指向index.d.ts文件。