Typescript找不到类型定义

Typescript找不到类型定义

问题描述:

我制作了一个我在npm上发布的打字稿模块,但是我无法让intellisens工作。Typescript找不到类型定义

文件结构是这样

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'; 

它将工作,没有错误,但没有intellisens,消息是the type definition is not found

但是,如果我这样做:

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

我得到intellisens。

在的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手册中的publishing部分。

在你package.json

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

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