我刚刚开始前端 Web 开发(所有这些包管理器/捆绑程序的 javascript hell ),我正在尝试使用 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')
因为它是单个函数导出。无论如何,在您首先修复后,您都会收到有关此问题的错误。享受🌹
关于javascript - 使用 Node 导入 typescript ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39710107/