I am having an issue with importing declarations from an extended file (I am using this typing). According to example, I should put this into my code:
import * as SockJS from 'sockjs-client';
import BaseEvent = __SockJSClient.BaseEvent;
import SockJSClass = __SockJSClient.SockJSClass;
However, when I try to do this as following:
module Test {
import * as SockJS from 'sockjs-client';
import BaseEvent = __SockJSClient.BaseEvent;
import SockJSClass = __SockJSClient.SockJSClass;
export class Example {
constructor() {......
}}}
I get the following error from the compiler:
error TS1147: Import declarations in a namespace cannot reference a module.
Am I doing something wrong? Or is there any issue with the typing itself?
Thanks
uksz
Use import myFunction from "./myModule" to bring it in. More commonly, TypeScript modules say export myFunction in which case myFunction will be one of the properties on the exported object. Use import { myFunction } from "./myModule" to bring it in.
With TypeScript 3.8, you can import a type using the import statement, or using import type .
TypeScript includes declaration files for all of the standardized built-in APIs available in JavaScript runtimes. This includes things like methods and properties of built-in types like string or function , top-level names like Math and Object , and their associated types.
To import a class from another file in TypeScript: Export the class from file A , e.g. export class Employee {} . Import the class in file B as import { Employee } from './another-file' . Use the class in file B .
You should use your import statements outside your module
import * as SockJS from 'sockjs-client';
import BaseEvent = __SockJSClient.BaseEvent;
import SockJSClass = __SockJSClient.SockJSClass;
module Test {
export class Example {
constructor(){}
}
}
I believe this is due to a mix of the typescript module options.
Your class uses internal modules and the typing file uses external modules. See the Working with other JavaScript libraries section here: http://www.typescriptlang.org/docs/handbook/modules.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With