Ced
Ced

Reputation: 17327

Typescript no type definition found

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/**"
  ]
}

Upvotes: 1

Views: 5895

Answers (1)

dotcs
dotcs

Reputation: 2296

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.

Upvotes: 3

Related Questions