Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a Javascript Function from Typescript

I am trying to build a nice windows phone application with an HTML front end. I want to use TYPESCRIPT to do my processing onto my html page. There is one javascript function which is crucial for my application to work - window.external.notify

This method isn't created till runtime I assume, so I build a javascript wrapper function to determine if it exists when it is called.

if (window.external.notify != undefined)
    window.external.notify(msg);

The issue is I need to get my Typescript files to see this function. I am using Visual Studio 2012 and I have seen the post - How to use an exported function within the local module The issue is when I just include my javascript file with the function I want to use I get the error TS2095.

error TS2095: Build: Could not find symbol

Any ideas or help or possible SDKs to circumvent this issue?

like image 422
MrSteamfist Avatar asked Feb 04 '14 05:02

MrSteamfist


People also ask

Can we call JavaScript function from TypeScript?

TypeScript supports the existing JavaScript function syntax for declaring and calling it within the program or the code snippet.

Can JavaScript and TypeScript work together?

The TypeScript compiler supports a mix of JavaScript and TypeScript files if we use the compiler option --allowJs : TypeScript files are compiled. JavaScript files are simply copied over to the output directory (after a few simple type checks).

How do you pass a function in TypeScript?

Similar to JavaScript, to pass a function as a parameter in TypeScript, define a function expecting a parameter that will receive the callback function, then trigger the callback function inside the parent function.


1 Answers

//notif.js

let notify = function(message) {
    alert(message);
}

//test.ts

declare function notify(message: string): any;
if(notify != undefined)
    notify("your message");

Make sure notif.js is loaded first.

like image 59
JFAP Avatar answered Sep 21 '22 01:09

JFAP