Angular2 - how to properly export and import a text definition file from typescript?

Suppose I have a library published in NPM with only one line:

// file name index.js
function myLib(x) {return x;}

Want to import it into app.component.tsthis repo

https://github.com/AngularClass/angular2-webpack-starter/blob/master/src/app/app.component.ts

After npm install myLiband create the file index.d.tsin node_modules/myLib/as

export default function myLib (x: any): any;
// or 
// declare function myLib (x: any): any;
// export default myLib;

Below are my QUESTIONS:

Q1.Use it in app.component.tslike:

import myLib from 'myLib'
console.log( myLib )

Result undefinedwhy?

Q2.If you change the code as below:

import * as myLib from 'myLib'
console.log( myLib )

The result function myLib(x) {return x;}seems correct, but

console.log( myLib(123) )

An error message appears in Webpack:

Src / app / app.component.ts: 12: 21

Cannot invoke an expression whose type does not have a call signature

What the heck?

Thank!!

+4

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


All Articles