Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method/property does not exist

I'm trying to convert the JavaScript code

if (window.ifEdit.editIsDirty()) { }

into Typescript. I got as far as the following

var iframe = document.getElementById('ifEdit');
var iWindow = <HTMLIFrameElement>(iframe).contentWindow;

var _editIsDirty = iWindow.editIsDirty();

I get the red squiggles under 'contentWindow' and 'editIsDirty' saying the method/property does not exist on the type. The .ts doesn't compile to a .js file.

I have searched, but did not manage to find a solution.

like image 416
Chris C. Avatar asked Dec 06 '22 18:12

Chris C.


1 Answers

For the contentWindow part, the problem with your code is that the casting is done wrong, should be:

var iWindow = (<HTMLIFrameElement> iframe).contentWindow;

As for the editIsDirty, it's not a standard property of Window.
If it's something which is added in the environment in which you are running your javascript then you need to declare it like so:

interface IfEdit {
    editIsDirty(): boolean;
}

interface Window {
    ifEdit: IfEdit;
}

var iframe = document.getElementById("ifEdit");
var iWindow = (<HTMLIFrameElement> iframe).contentWindow;

var _editIsDirty = iWindow.ifEdit.editIsDirty();

Use the code in Playground.

like image 175
Nitzan Tomer Avatar answered Dec 09 '22 15:12

Nitzan Tomer