带节点的打字稿导入

typescript import with node

我刚开始使用前端 Web 开发(javascript 搞砸了所有这些包 manager/bundler),我正在尝试使用 typescript + browsify

所以我创建了一个 index.ts 文件并下载了 uniq 模块(使用 npm)只是为了测试 ts 文件编译

这是我的 index.ts

/// <reference path="node_modules/uniq/uniq.d.ts" />
import {unique} from "uniq";

var data = [1, 2, 2, 3, 4, 5, 5, 5, 6];

console.log(unique(data));

uniq.d.ts

// Type definitions for uniq
// Project: https://www.npmjs.com/package/uniq
// Definitions by: Hans Windhoff <https://github.com/hansrwindhoff>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped


interface Uniq{
  <T>(ip:Array<T>): Array<T>;
}

declare var uniq :Uniq;

declare module "uniq" {
export = uniq;
}

目录结构

.
├── entry.ts
├── node_modules
│   └── uniq
│       ├── LICENSE
│       ├── package.json
│       ├── README.md
│       ├── test
│       │   └── test.js
│       ├── uniq.d.ts
│       └── uniq.ts
└── package.json

但是当我尝试编译 index.ts 时,我得到了这个错误:

error TS2688: Cannot find type definition file for 'uniq'.

第一个

你的路径可能有误:

/// <reference path="node_modules/uniq/uniq.d.ts" />

也许 ../../node_modules/uniq/uniq.d.ts。请使用 tsconfig.json 而不是像这样的脆弱路径:https://basarat.gitbooks.io/typescript/content/docs/project/tsconfig.html

第二个

基于 .d.ts 你显示你的导入 import {unique} from "uniq"; 也是错误的。它应该是 import unique = require('uniq') 因为它是一个单一的函数导出。在您首先修复之后,无论如何您都会收到有关此的错误。享受