简体   繁体   English

如何在TypeScript中正确地要求和声明节点库类型?

[英]How do I correctly require and declare node library typings in TypeScript?

I am trying to write a package for node in TypeScript that uses standard node libraries, for example fs , path , stream , http , and so on. 我正在尝试在TypeScript中编写一个使用标准节点库的节点包,例如fspathstreamhttp等。

When I try to import libraries in a .ts file, VS Code marks the corresponding line with an error: 当我尝试在.ts文件中导入库时,VS Code会在相应的行中标记错误:
[ts] Cannot find module 'fs' . [ts] Cannot find module 'fs' This happens no matter how I try to import the library: 无论我如何导入库,都会发生这种情况:

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'

在此输入图像描述

I (should) have the correct definitions installed with typings install --save --ambient node . 我(应该) typings install --save --ambient node正确的定义typings install --save --ambient node

When I compile to JavaScript (using gulp and gulp-typescript , not tsc ), the compliation works and the syntax highlighting shows no errors until I type in 1 character again: 当我编译为JavaScript(使用gulpgulp-typescript ,而不是tsc )时,compliation 工作语法高亮显示没有错误,直到我再次输入1个字符:

在此输入图像描述

How can I correctly define the node libraries for TypeScript? 如何正确定义TypeScript的节点库?


I use VS Code for code highlighting and autocompletion, gulp & gulp-typescript to compile and typings for the typescript library declarations. 我使用VS Code进行代码突出显示和自动完成,使用gulpgulp-typescript进行编译,并使用typings库声明进行打字。

The project's directory structure: 该项目的目录结构:

├─ 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

My tsconfig.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"
    ]
}

My typings.json : 我的typings.json

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

And my gulp task: 我的gulp任务:

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

In case anyone has experienced the same problem, I am thankful for any insights. 如果有人遇到同样的问题,我很感谢任何见解。

the installation of type declarations with 'typings install ...' is obsolete since a few months. 几个月后,使用'typings install ...'安装类型声明已经过时了。 The new way is to install it directly via npm and the @types namespace. 新方法是通过npm和@types命名空间直接安装它。 to install the node type declarations just use npm install @types/node --save-dev . 安装节点类型声明只需使用npm install @types/node --save-dev

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM