繁体   English   中英

打字稿找不到类型定义

[英]Typescript no type definition found

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

文件结构就是这样

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

src包含.ts文件,而dist .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/**"
  ]
}

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

在你的package.json

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM