Typescript no type definition found

☆樱花仙子☆ 提交于 2021-02-10 15:12:51

问题


I made a typescript module that I published on npm, however I can't get intellisens to work.

The file structure is as such

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

src contains the .ts files, and dist the .js, .d.ts, .js.map.

If I do this:

import { X } from 'my-package';

It will work, no error, but no intellisens, and the message is the type definition is not found.

However if I do this:

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

I do get intellisens.

In package.json I put:

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

And here is my 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:


You need to reference the .d.ts file separately. See section publishing in the TypeScript handbook.

In your package.json:

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

Note the types key whose value points to the index.d.ts file.



来源:https://stackoverflow.com/questions/45574829/typescript-no-type-definition-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!