introducing strong typing over window document or other browser apis


This is just a demo but it shows how to introduce strong typing over browser api like document or even service worker api.

Anyways, lets use document as our example : In this example, we trying to create a simple TestBrowser class which uses interface BrowserDoc (for windows document type). 

In the BrowserDoc interface we can define all the browser interface like alert, queryElement, in this case, it is just write.

Then we initialize our TestBrowser and slot in our window.document.



 
class TestBrowser {

constructor(document : BrowerDoc) {
document.write('test');
}
}

interface BrowerDoc {
write(s : string) : void;
}

var a = new TestBrowser(window.document);

Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm