【问题标题】:How do I correctly require and declare node library typings in TypeScript?如何在 TypeScript 中正确要求和声明节点库类型?
【发布时间】:2016-08-17 01:41:36
【问题描述】:

我正在尝试为使用标准节点库的 TypeScript 中的节点编写一个包,例如 fspathstreamhttp 等。

当我尝试在 .ts 文件中导入库时,VS Code 将相应的行标记为错误:
[ts] Cannot find module 'fs'。 无论我如何尝试导入库,都会发生这种情况:

import * as fs from 'fs';    // [ts] Cannot find module 'fs'
import fs = require('fs');   // [ts] Cannot find module 'fs'
const fs = require('fs');    // [ts] Cannot find name 'require'

我(应该)使用typings install --save --ambient node 安装了正确的定义。

当我编译为 JavaScript(使用 gulpgulp-typescript,而不是 tsc)时,编译有效语法高亮显示没有错误,直到我再次输入 1 个字符:

如何正确定义 TypeScript 的节点库?


我使用 VS Code 进行代码高亮和自动补全,gulp & gulp-typescript 用于编译,typings 用于 typescript 库声明。

项目的目录结构:

├─ build/
│  └─ (output files)
├─ src/
│  └─ myfile.ts
├─ typings/
│  ├─ browser/
│  │  └─ ambient/
│  │     └─ node/
│  │        └─ index.d.ts
│  ├─ main/
│  │  └─ ambient/
│  │     └─ node/
│  │        └─ index.d.ts
│  ├─ browser.d.ts
│  └─ main.d.ts
├─ gulpfile.js
├─ package.json
├─ tsconfig.json
└─ typings.json

我的tsconfig.json

{
    "compileOnSave": false,
    "compilerOptions": {
        "declaration": true,
        "module": "system",
        "moduleResolution": "node",
        "noEmitOnError": true,
        "noImplicitAny": true,
        "target": "es5"
    },
    "exclude": [
        "node_modules",
        "typings/browser",
        "typings/browser.d.ts"
    ]
}

我的typings.json

{
  "ambientDependencies": {
    "node": "registry:dt/node#4.0.0+20160412142033"
  }
}

还有我的吞咽任务:

gulp.task('build-typescript', () => {
    const gulpts = require('gulp-typescript');
    const tsProject = gulpts.createProject('tsconfig.json', {
        typescript: require('typescript'),
        outFile: 'mylibrary.js',
        noLib: true
    });

    let tsstream = (
        gulp.src([
            'node_modules/typescript/lib/lib.es6.d.ts',
            'typings/main.d.ts',
            'src/sharpscript.ts'])
        .pipe(sourcemaps.init())
        .pipe(gulpts(tsProject))
    );

    return require('merge2')(
        tsstream.dts.pipe(gulp.dest('build')),
        tsstream.js
            .pipe(sourcemaps.write('.', { includeContent: true }))
            .pipe(gulp.dest('build'))
    );
});

如果有人遇到同样的问题,我很感谢您提供任何见解。

【问题讨论】:

  • tsc 命令的结果是什么?
  • 如果你使用的是旧版本的 VSCode,你必须在 VSCode 中运行“Reload Typescript Project”命令。
  • @alisabzevari 我没有使用tsc 进行编译,因为我将项目编译为mylibrary.js,并将测试(在同一文件夹中)编译为mylibrary.tests.js,这对于tsc 是不可能的.我使用gulp-typescript
  • 如果你确实想要一个文件 - 使用命名空间,但我仍然建议以 commonjs 为目标 - 因为这是 node 使用的模块加载器标准。
  • 我刚刚试用了我的项目中报告的问题。无法重现。我今天添加了一个使用 Node 和 typescript 的简单项目。您可以克隆项目并检查问题是否仍然存在吗? github.com/rmchndrng/XprssNg2TSSeed 我的 Visual Studio Code 版本详细信息 版本 1.0.0 提交 fa6d0f03813dfb9df4589c30121e9fcffa8a8ec8 日期 2016-04-13T14:08:36.599Z Shell 0.35.6 渲染器 45.0.2454.85 节点 4.1.1

标签: node.js typescript visual-studio-code definitelytyped


【解决方案1】:

使用 'typings install ...' 安装类型声明已经过时了几个月。新方法是直接通过 npm 和 @types 命名空间安装它。要安装节点类型声明,只需使用npm install @types/node --save-dev

【讨论】:

    猜你喜欢
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 2019-06-27
    • 2018-03-11
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多