node.js - 如何导出和导入类型定义?

标签 node.js typescript

我使用 TypeScript 一段时间了,模块系统对我来说仍然是个谜。

我有这个类型定义文件(appComponents.d.ts):

/// <reference path="./authentication/API.d.ts"/>

import express = require('express');

declare module appComponents {
    export interface IComponents {
        application: express.Application;
        authenticationService: MyApp.IAuthenticationService;
        // and so on ...
    }
}

和这个文件(index.ts):

/// <reference path="./appComponents.d.ts"/>

import express = require('express');
import mssql = require('mssql');

function initComponents(): appComponents.IComponents {

    // Components initialized here ...
}

两个问题:

  1. 为什么我必须使用

    import express = require('express');

    而不是

    /// <reference path="./path/to/definitely-typed/express/express.d.ts"/>

    避免error TS2095: Could not find symbol 'express'. ?毕竟,这只是一个不生成 JavaScript 的类型定义文件,它依赖于另一个也不生成 JavaScript 的类型定义文件中的类型。

  2. 为什么 index.ts原因error TS2095: Could not find symbol 'appComponents'. ?当我这样做时:

    import appComponents = require('./appComponents');

    为什么会导致error TS2094: The property 'IComponents' does not exist on the value of type 'appComponents'.

使用 TypeScript 0.9.7.0。

最佳答案

  1. ) Why do I have to use

import express = require('express'); 代替 /// <reference path="./path/to/definitely-typed/express/express.d.ts"/>

实际上您需要同时使用两者:

/// <reference path="./path/to/definitely-typed/express/express.d.ts"/>
import express = require('express');

您可能在 API.d.ts 中找到了引用资料或者只是有 express.d.ts包含在您的 Visual Studio 项目中的某个位置。

工作原理:https://github.com/borisyankov/DefinitelyTyped/blob/master/express/express.d.ts#L15包含declare module "express" {这告诉 typescript 要提供什么(此文件中带有export的所有内容:https://github.com/borisyankov/DefinitelyTyped/blob/master/express/express.d.ts当有人这样做时import / requireimport express = require('express')这些在 typescript 中被称为外部模块,可以是amd/commonjs

Why does index.ts cause error TS2095: Could not find symbol 'appComponents'

因为您正在声明一个内部模块并尝试将其作为外部模块导入。

PS:有关外部/内部模块的视频:http://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1

更新

You said that one of my modules in the question is an internal module:

仅供引用:declare module appComponents {使appComponents一个内部模块。你应该这样做declare module "appComponents" {如果你想声明一个外部模块并使用 import appComponents = require('appComponents');但不要。这不是你想要的。

Why does index.ts cause error TS2095: Could not find symbol 'appComponents'.?

因为appComponents.d.tsimport它也成为一个外部模块。你应该搬家declare module appComponents {等到其自己的文件,不含外部模块,然后使用 ///<reference

关于node.js - 如何导出和导入类型定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22164864/

相关文章:

javascript - JSDoc + IDE 与 TypeScript

node.js - 在 lambda 函数中使用 https 调用和 Nodejs 返回数据

javascript - Selenium Webdriver 无法在 Electron 构建中工作

reactjs - 调度问题 "Argument of type '(调度 : Dispatch<T>) => Promise<void >' is not assignable to parameter of type ' T'.”

Angular 2. 重建表单控件

reactjs - 使用 onBlur 在 React 组件中设置焦点

node.js - 具有对私有(private)存储库的只读访问权限的 GitHub 访问 token

javascript - 对象预期的 Microsoft Jscript 运行时错误 - Node js

node.js - 从数组获取最新的子文档

android - Ionic 4/Cordova - 获取设备帐户