如何在 VSCode 中使用 TypeScript 让我的模块使用智能感知

How to make intellisense work for my modules using TypeScript in VSCode

我正在 VSCode.

中使用 nodejs 和 electron 构建一个应用程序

我目前使用 javascript,但我想切换到打字稿以获得更好的代码验证和智能感知。

Intellisense 对于外部模块(electron、lodash、node 核心模块)工作正常,但我无法让它与我自己的模块一起工作。

我已经添加了我的模块(我只在下面的代码中显示了其中一个)作为我的依赖项 package.json:

"dependencies": {
    ...
    "renderer-rpc": "file:local_modules/renderer-rpc",
    ...
  }

这是renderer-rpc.ts的内容(只是相关代码):

import {ipcMain} from 'electron'
import {ipcRenderer} from 'electron'


export const mainRpc = {
    init : function () {
        ...
    }
}

export const rendererRpc = {

    init : function (options) {
        ...
    },

    call : function (recipient, method, ...args) {
        ...
    },

    makeRemote: function (object, recipient) {
        ...
    }
}

然后在我的 main.ts(位于项目的根目录)中,我执行以下操作:

import {mainRpc} from 'renderer-rpc'

mainRpc.

而且我没有从我的模块中得到任何建议。既不按 CTRL+SPACE.

这是我的tsconfig.json

{
    "compilerOptions": {
        "target": "ES6",
        "module": "commonjs",
        "sourceMap": true,
        "watch": true
    },
    "exclude": [
        "node_modules",
        "**/node_modules/*"
    ]
}

这是我的 package.json:

{
  "name": "image-gallery",
  "version": "1.0.0",
  "private": true,
  "main": "main.js",
  "scripts": {
    "test": "mocha",
    "start": "electron ."
  },
  "author": "Mic",
  "license": "ISC",
  "dependencies": {
    "@types/node": "^8.5.1",
    "DAL": "file:local_modules/DAL",
    "async-parallel": "^1.2.3",
    "bootstrap": "^3.3.7",
    "db-layer": "file:local_modules/db-layer",
    "electron-pug": "^1.5.1",
    "jimp": "^0.2.28",
    "jquery": "^3.2.1",
    "lodash": "^4.17.4",
    "nedb": "^1.8.0",
    "pug": "^2.0.0-rc.4",
    "q": "^1.5.1",
    "renderer-rpc": "file:local_modules/renderer-rpc",
    "scattered-store": "^1.0.0"
  },
  "devDependencies": {
    "electron": "1.7.9",
    "mocha": "^4.1.0",
    "mock-require": "^2.0.2",
    "typescript": "^2.6.2"
  }
}

将以下内容添加到 "renderer-rpc" 包中的 tsconfig:

"declaration": true,

这将强制打字稿编译器在编译期间创建声明 (.d.ts) 文件。

还有一件事,在 "renderer-rpc" 中,您应该将其添加到您的 package.json 中(修改路径以指向您的主打字文件):

 "typings": "./dist/index.d.ts"