Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

javascript - How can I declare a function from another file in Typescript?

I have the following function in a file:

function alertWin(title, message) {
   .......
   .......
}

In another typescript file I have:

function mvcOnFailure(message) {
    "use strict";
    alertWin("Internal Application Error", message);
}

I am getting an error saying "alertwin" does not exist in the current scope.

Is the way to solve this for me to define this function in another file and then reference that? If so then what would the definition look like?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do this (assuming title and message are both supposed to be strings):

interface alertWinInterface{
    (title:string, message: string):any;
}

declare var alertWin: alertWinInterface;

You could put this in the same file, or put it in a separate ambient definitions file (.d.ts) which you import:

/// <reference path="myDefinitions.d.ts" />

Or, you could just import the other file that has the actual function definition, but you won't get static typing support.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...