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

javascript, typescript

Solution

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.

Problem

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?

Original source