10

I have an Angular2 project where i need to import a javascript file for use in my typescript.

I have a javascript file in app/js/d3gantt.js which contains a single function of gantt = function().

gantt = function() {
    //Does lots of stuff
    return gantt;
};

I then have a definition file called d3gannt.d.ts in the same folder which looks like this:

declare module 'd3Gantt' {
    export module d3Gantt {
        export function gantt(): any;
    }
}

And then i reference it in my component as import * as d3Gantt from '../app/js/d3gantt';

However, i get the error message stating File 'c:/Projects/RepackLog/RepackLog/RepackLog/app/js/d3gantt.d.ts' is not a module

Am i missing something that is needed for this to pick up my files properly?

Thanks,

2
  • You need to export, export gantt; on your **app/js/d3gantt.js ** Commented Oct 28, 2016 at 9:48
  • OK where do i put it and whats the syntax? ive put my js file in question removing the contents of the function so they dont get in the way Commented Oct 28, 2016 at 9:51

2 Answers 2

3

As @FabioAntunes mentioned in one of the comments above, you just have to export gantt on your d3gantt.js file itself, in-line. The exact syntax would be

export gantt = function() {
    //Does lots of stuff
    return gantt;
};

No need to declare it anywhere else. For further note on exporting modules please refer this post (Typescript es6 import module "File is not a module error").

Sign up to request clarification or add additional context in comments.

Comments

1

Declare module as below :

declare module 'd3Gantt' {
  export function gantt(): any;    
}

Then import module as : import * from 'path to your module file' or import * as d3 from 'path to your module file'

5 Comments

change file name to d3gantt.ts and check again
the javascript file?
you are importing with import * as d3Gantt from '../app/js/d3gantt' and you have said that app/js/d3gantt is app/js/d3gantt.d.ts so change its name to d3gantt.ts
no it didnt work. am i missing something to be able to map a javascript file to typescript
check stackoverflow.com/questions/40259765/… also may it will help you

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.