1

I am having trouble importing declared global variable.

On our website, we have third party framework, which integrates only using tag and creates global variable on window for example window.thirdParty. So usage is like: thirdParty.methodOne() I wrote definition file third-party.d.ts which is like this:

import { ITrirdPartyFramework } from './third-party-definitions/third-party-framework';

export interface ITrirdPartyFramework {
   methodOne():void,
}

then i have a file (global-variables.ts), which defines global variables:

export declare const thirdParty: ITrirdPartyFramework;

and finally i want to use it in my modules:

import { thirdParty } from './global-variables';

export const myFunction = () => {
    thirdParty.methodOne();
}

Problem is that webpack compiles the definitions as regular modules and on runtime i get error, because compiled code looks like this:

_global_variables__WEBPACK_IMPORTED_MODULE_3__["thirdParty"].methodOne();

instead of

thirdParty.methodOne();

Do you know, how to tell webpack to ignore the definitions or how to deal with this situation ?

5
  • define your globals in *.d.ts file Commented Sep 23, 2019 at 14:50
  • Thanks for tip, i tried it but webpack doesnt compile, it cant find the module: Module not found: Error: Can't resolve './global-variables' in 'my-code-file.ts'. Commented Sep 23, 2019 at 14:57
  • You don't have to import *.d.ts files, they are globally visible. You can keep only type definitions in those files, but not implementation Commented Sep 23, 2019 at 15:07
  • 1. Rename global-variables.ts to global-variables.d.ts. 2. remove export keyword from global-variables. 3. remove line "import { thirdParty } from './global-variables';" Commented Sep 23, 2019 at 15:25
  • Thanks a lot @Artem, it really solved my problem. But i cant accept comment as answer. If you want, you can post the answer and i will accept it, or i am going to rewrite you answer tomorrow. Commented Sep 24, 2019 at 7:11

1 Answer 1

2

You don't have to import *.d.ts files, they are globally visible.
You can keep only type definitions in those files, but not implementation

Next steps will solve your problem:

  1. Rename global-variables.ts to global-variables.d.ts
  2. remove export keyword from global-variables.d.ts
  3. remove line import { thirdParty } from './global-variables';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.