【问题标题】:Using a Typescript .d.ts file that doesn't declare a module使用未声明模块的 Typescript .d.ts 文件
【发布时间】:2015-11-22 06:18:49
【问题描述】:

我正在使用 tsd 从“绝对类型化”下载定义并编译成 tsd.d.ts 文件。我还不能构建,但是当我使用这样的导入时:

import * as THREE from "three"

Visual Studio 智能感知很高兴。但是,这不适用于 Detector.js(用于检测 webgl 支持的 three.js 库),带有 this .d.ts 文件。我不确定问题是什么,但我确实注意到了 three.d.ts 文件导出 一个模块(三个)和detector.d.ts 文件只导出一个对象:

三个.d.ts

...
declare module 'three' {
    export=THREE;
}

检测器.d.ts

interface DetectorStatic {
    canvas: boolean;
    webgl: boolean;
    workers: boolean;
    fileapi: boolean;

    getWebGLErrorMessage(): HTMLElement;
    addGetWebGLMessage(parameters?: {id?: string; parent?: HTMLElement}): void;
}

declare var Detector: DetectorStatic;

这会改变我应该如何导入检测器吗?

【问题讨论】:

  • 你的问题很不清楚。请提供一个具体的场景和尽可能多的细节。代码也不错。

标签: three.js typescript definitelytyped tsd


【解决方案1】:

对于这种情况,您可以自己定义。对于大多数项目,我通常有一个临时的 .d.ts 文件,我会抛出我需要的随机声明(小型接口、模块等)。

您应该能够在代码中的某处简单地定义模块。

declare module "path/to/detector" {
    export = DetectorStatic;
}

【讨论】:

  • 好的。谢谢!这就说得通了。但是,为什么绝对类型文件不包括这个?有没有其他方法可以使用不需要使用模块的.d.ts 文件?
  • 在我的情况下,我不确定这个答案是否正确,但它非常接近。我在我的问题中添加了一些细节。我发现可以工作的是:在临时 custom.d.ts 文件中放入 declare module "Detector" { export = DetectorStatic; } 在我的 tsd.d.ts 文件中添加一行:/// <reference path="custom.d.ts" />。要导入文件,请使用import Detector = require("Detector"); 而不是import {Detector} from "Detector";(最后一部分的详细信息here)。
猜你喜欢
  • 1970-01-01
  • 2020-11-22
  • 2020-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-18
  • 2019-07-12
相关资源
最近更新 更多