pablorsk pablorsk - 3 months ago 18
TypeScript Question

Use javascript libraries installed by npm

I'm sorry, but I'm very lost. Actually I have this:

import * as angular from 'angular';
import 'ts-angular-jsonapi';


And dont return any errors. But, when I do this

import * as angular from 'angular';
import * as jsonapi from 'ts-angular-jsonapi';


I get this error:

ERROR in ./src/index.ts
(14,23): error TS2307: Cannot find module 'ts-angular-jsonapi'.


What changes need I do on
ts-angular-jsonapi
library for fix this errors.

More info: I need do this becouse I need make something like this:

class myresource extend jsonapi.resource {

}

Answer

I found the solution. When you write a .TS file, you can not import any .JS file. You need use require(). In the code:

// some_file.ts
import * as angular from 'angular';
var jsonapi = require('ts-angular-jsonapi');  // .js library

But if you are searching the best and correct solution for typescript node module, I build a simple example on github. With this example, you can do something like this:

import * as animal_module from 'animal_module';

class Snake extends animal_module.Animal {
    constructor(name: string) { super(name); }
}

let sam = new Snake('Sammy the Python');
Comments