17

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

1
  • 1
    did you reference the d.ts file as /// <reference path="./sockjs-client.d.ts" /> or in common tsd.d.ts and keep the import out of the module Commented Nov 19, 2015 at 12:30

4 Answers 4

13

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(){}
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

As soon as I put it outside my namespace, everything else starts to break. I guess it's causing my code to be registered as an external module and then it can no longer see other third-party libraries. Is there something I can do to fix that? Thanks
@AdamPlocher I have the same problem. Did you find any solution?
@romanoza I stopped using module or namespace. I was racking my brain trying to remember why, but I came across an answer I wrote to another question (just 2 days after this comment, which I apparently forgot about). It seems to address this topic exactly: stackoverflow.com/a/43023392/730566
4

I succeeded to use imported typings in my customizing Cypress code using the same syntax as them :

declare global {
    namespace Cypress {

https://github.com/cypress-io/cypress/blob/3e8b52f3deb82b5d29b959cbbe01917010fa5f92/packages/net-stubbing/lib/external-types.ts#L368

So it works at least in a namespace, not tested in a ts module but I hope it does too.

It seems to only work in this certain context because if I comment out my imports it throws: Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.ts(2669). But if you are there, that's you want to import/export something.

Comments

2

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

1 Comment

Do you have any ideas how to fix it?
0

I have faced the same issue when trying to import moment.d.ts type definition file in one of my typescript file.

I'm also wrapping my whole class inside a module. The solution that I did to solve the issue was, in my typescript file - Scheduler.ts, I put the line import * as moment from "../definitions/moment/moment"; just before the module declaration (refer to image below).

I didn't include the whole class definition for brevity reason.

enter image description here

As you can see, I have reference path explicitly define in the typescript file for external libraries (jquery, kendo ui and moment).

The folder structure where type definitions are saved.

enter image description here

Below is also my tsconfig.json, not quite sure setting this allowSyntheticDefaultImports: true ease the problem. I just followed the notes written on this link when importing and using in typescript file.

    {
  "compileOnSave": true,
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5",
    "module": "commonjs",
    "strictNullChecks": false,
    "allowSyntheticDefaultImports": true
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.