打字稿导入节点

我只是从前端网页开发(所有这些包pipe理器/打包器JavaScript的地狱),我试图使用打字稿+ browsify

所以我创build一个index.ts文件并下载uniq模块(使用npm)来testingts文件的编译

这里是我的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'. 

第一

你可能有错误的path:

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

也许../../node_modules/uniq/uniq.d.ts 。 而不是像这样的britlepath,请使用tsconfig.json : https : tsconfig.json

第二

根据.d.ts您显示了import {unique} from "uniq";导入import {unique} from "uniq"; 也是错的。 它应该是import unique = require('uniq')因为它是一个单一的函数导出。 首先修复后,你会得到一个关于这个错误。 享受🌹

Interesting Posts