admin管理员组

文章数量:1357082

Let's say I have a javascript file, myScript.js, with the following content:

function run(file) {
    // some operations
}

How can I use the 'run' function inside a TypeScript file?

I've read similar questions but didn't find their answers clear enough: where I declare the function? how do I reference the declaration file etc..

Let's say I have a javascript file, myScript.js, with the following content:

function run(file) {
    // some operations
}

How can I use the 'run' function inside a TypeScript file?

I've read similar questions but didn't find their answers clear enough: where I declare the function? how do I reference the declaration file etc..

Share Improve this question edited Jul 4, 2016 at 10:33 yonisha asked Jul 4, 2016 at 10:03 yonishayonisha 3,0962 gold badges26 silver badges33 bronze badges 3
  • You need to write a declaration file for that script – Nitzan Tomer Commented Jul 4, 2016 at 10:05
  • 1 Possible duplicate of Using a 3rd party js file with TypeScript – Nitzan Tomer Commented Jul 4, 2016 at 10:06
  • I think that the other answers are not details enough. Where should I declare it? where is the reference to the js file? That's the information I'm looking for – yonisha Commented Jul 4, 2016 at 10:21
Add a ment  | 

1 Answer 1

Reset to default 5

TypeScript does not know about the run function. You'll have to declare it:

declare function run(file: any): any;

Now this works:

run(myFile);

EDIT: you can also look into using "--allowJs" tsc parameter mentioned here, but I have no experience with it.

EDIT 2: If the external script is a published library, chances are that there is a typings file (.d.ts) for it. Check out the typings tool in that case.

本文标签: Using external javascript script in TypeScriptStack Overflow