TypeScript: import an external module from node_modules

For example, the npm one-two-three module. It contains TS index.ts (core) and functions.ts files.

functions.ts:

 export interface IPoint { x: number; y: number; } export function sum(a: IPoint, b: IPoint): IPoint { return { x: ax + bx, y: ay + by }; } 

index.ts:

 import functions = require("./functions"); export var sum: typeof functions.sum = functions.sum; 

compilation:

 tsc --module commonjs --declaration index.ts 

Files are created: index.js , index.d.ts , functions.js and functions.d.ts . Good.

There is another library that depends on one-two-three .

 npm install --save one-two-three 

I want to include a dependency and use it and the interface from functions.ts .

 import mo = require("one-two-three"); 

Cannot find external module 'one-two-three' error.

 /// <reference path="node_modules/one-two-three/index.d.ts" /> import mo = require("one-two-three"); 

There is no reaction.

 import mo = require("./node_modules/one-two-three"); 

Failure.

 declare var require; var mo = require("one-two-three"); 

It compiles successfully. But there is no type check. You can write: mo.unknownFunction() , and it will be compiled. Unable to use interfaces.

How to do the above in the correct?

UPDATE

I have achieved the desired behavior. Edit the d.ts files.

functions.d.ts:

 declare module "one-two-three.functions" { export interface IPoint { x: number; y: number; } export function sum(a: IPoint, b: IPoint): IPoint; } 

index.d.ts:

 /// <reference path="./functions.d.ts" /> declare module "one-two-three" { import functions = require("one-two-three.functions"); export var sum: typeof functions.sum; } 

Using it:

 /// <reference path="node_modules/one-two-three/index.d.ts" /> /// <reference path="node_modules/one-two-three/functions.d.ts" /> import oneTwoThree = require("one-two-three"); import functions = require("one-two-three.functions"); import IPoint = functions.IPoint; function delta(a: IPoint, b: IPoint): number { var dx: number = ax - bx, dy: number = ay - by; return Math.sqrt(dx * dx + dy * dy); } var point1: IPoint = {x: 10, y: 20}, point2: IPoint = {x: 5, y: 5}; console.log(oneTwoThree.sum(point1, point2)); console.log(delta(point1, point2)); 

Success. But we must fulfill a double duty. Write the code and separately describe the interface.

Is there a way to generate the correct d.ts? The problem is that d.ts must describe a module with internal syntax ( module {} ). But the source file is the CommonJS module. It does not have a module section.

+6
source share
1 answer
 /// <reference path="node_modules/one-two-three/index.d.ts" /> import mo = require("one-two-three"); 

There is no reaction.

It should work.

A TypeScript .d.ts file is similar to a .h file in C. It is usually used when a dependency is imported from another project or subproject.

If the file your-project/node_modules/one-two-three/index.d.ts is not written correctly, I suggest copying it to your-project/one-two-three.d.ts , and then correct the copy. Using the module name as the file name makes /// <reference optional. Just write:

 import mo = require("one-two-three"); 
+4
source

Source: https://habr.com/ru/post/985188/


All Articles