Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript issue with importing declarations

Tags:

typescript

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

like image 424
uksz Avatar asked Nov 19 '15 11:11

uksz


People also ask

How do I enable imports in TypeScript?

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.

Does TypeScript support import?

With TypeScript 3.8, you can import a type using the import statement, or using import type .

What is TypeScript declaration files?

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.

How do I import files into TypeScript?

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 .


2 Answers

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(){}
    }
}
like image 146
Andzej Maciusovic Avatar answered Sep 19 '22 06:09

Andzej Maciusovic


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

like image 22
Bjørn Sørensen Avatar answered Sep 19 '22 06:09

Bjørn Sørensen