Let me give a possible workaround for people who are using a third party (npm) javascript module for which there is no typings definition and want to get rid of the "cannot find module" error message
Suppose you use the npm package jsonapi-serializer and you reference it in your example.ts
file:
import {Serializer, Deserializer} from "jsonapi-serializer";
@Injectable()
export class ExampleService {
var deserializer = new Deserializer({ keyForAttribute: 'camelCase'});
}
The compiler will complain: Error:(1, 40) TS2307:Cannot find module 'jsonapi-serializer'.
You can declare the typings definition yourself by creating the following file: jsonapi-serializer.d.ts
. You have to specify the main functions, but using the any
type you can keep it relatively simple. (Consider donating the definition file if you create a complete definition):
declare module 'jsonapi-serializer' {
export class Serializer {
constructor(type: string, ...options: any[]);
serialize(arg: any)
}
export class Deserializer {
constructor(...options: any[]);
deserialize()
}
}
If you place this file in your project the compiler will recognize the module. When this doesn't work you can also reference it from your ExampleService.ts
file by placing the following line on top of the file:
///<reference path="jsonapi-serializer.d.ts"/>