karim karim - 2 months ago 36
TypeScript Question

typescript import with node

I'm just starting with front-end web developpement(javascript hell with all these package manager/bundler) and I'm trying to use typescript + browsify

so I create a index.ts file and download the

uniq
module(using npm) just to test ts file compilation

here is my 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;
}


directory structure

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


but when I try to compile the index.ts I got this error :

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

Answer

First

you probably have the path wrong:

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

Maybe ../../node_modules/uniq/uniq.d.ts. Instead of britle paths like this please use tsconfig.json : https://basarat.gitbooks.io/typescript/content/docs/project/tsconfig.html

Second

Based on the .d.ts you showed your import import {unique} from "uniq"; is also wrong. It should be import unique = require('uniq') since its a single function export. You will get an error about this anyways after you fix first. Enjoy

Comments