Type Error on Valid JS code with HTML Type { Wrong Error throwing }
TypeScript Version: 2.0 {beta} IDE Visual studio community 2015
Summary of the problem Multiple code who are VALID Javascript code throw errors in Typescript
Example of code from a stable and working code (it's totally work in Javascript without any problem at all) ` private static _requestFullScreen() {
let element = document.body;
if (element.requestFullScreen) {
element.requestFullScreen();
} else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if (element.webkitRequestFullScreen) {
element.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
} else if (element.msRequestFullscreen) {
element.msRequestFullscreen();
}
}`
it's will show a error in Typescript saying : "requestFullScreen does not exist on type HTMLElement" when it's actually exist Link of the reference who say it's a valid element in Javascript : here
or even when using this :
let box = document.createElement('div'); box.style.zIndex = 9;
it's will say that zIndex is not assignable with number and the definition say it's a string
Although it's a valid Javascript code who will work just fine.
My typescript configurations It's a classic TsConfig Json, I use CommonJS and the only exclude module is "Node_Module"
Conclusion I hope it's enough clear and I'm sorry if my english is not really pretty. I'm not a Native english. all Regards Nio Kasgami
this can be shortened to this test case:
document.body.requestFullscreen();
// OK
var element = document.body;
element.requestFullScreen();
// [ts]
// Property 'requestFullScreen' does not exist on type 'HTMLElement'.
// any
It isn't included in the typings, because it only exists in most browsers in a prefixed fashion and as MDN explains, in Chrome is it considered an experimental features. Currently only Edge has it implemented as requestFullScreen and in Chrome it is webkitRequestFullScreen, Firefox mozRequestFullScreen (unless you turn on the unprefixed flag), as your code demonstrates.
When doing these sorts of feature detections like this for inconsistent browser APIs, you can cast it as <any> to avoid the interface checking. If you are confident your code is safe, you can of course extend the global interface (as they are open ended):
declare global {
interface Element {
requestFullScreen?(): void;
}
}
const element = document.body;
element.requestFullscreen(); // No Error...
@kitsonk it is on typings, indeed the first line of my snippet above does not give error.
There are two different casings present in the examples here: requestFullscreen and requestFullScreen (note the 's' in 'fullscreen').
The correct casing according to MDN seems to be the one with the lower case 's'.
lib.d.ts does define requestFullscreen but not the vendor prefixed forms (moz- ms- etc).
@nippur72 I see well the code is from Yoji Ojima the creator of Rpg Maker as we know the latest Game Engine is made with Pure JS well the Library Framework (the Game API wich you can edit freely for your game use) I'm always kinda reluctant to edit it because those are the Game API core code who make the game run but I will try to adjust with your code thanks!
@kitsonk I see thanks I didn't know I'm not THAT well versed with Browser Api I mostly code for Game side and I'm still new to the Browser Api sorry for this. And thanks for showing a example of how fix this!
Only one question though : Why declaring it in the Global? when I can actually do it directly from "Element" interface? Does it's a design choice from you?
@yortus I see so it's better I define myself in a declaration file?
and thanks you guys for being patient with me I'm kinda still new to typescript.
all regards Nio Kasgami
@kitsonk, @yortus oops sorry for the noise, I did not realize there was different letter casing !! :flushed:
@niokasgami yes, it's better you augment the Element interface, where you can put all the missing methods (mozRequestFullScreen, msRequestFullScreen, etc..)
Only one question though : Why declaring it in the Global? when I can actually do it directly from "Element" interface? Does it's a design choice from you?
I demonstrated a way to do it from within a module. It can be placed in a global .d.ts as well without that. If you were writing a module and declared an interface Element it would have only been scoped within that module, just like a local variable can cause a global variable to be masked. Therefore if you are in a module of functional code, that is the way you can extend a global interface.
It is therefore a style choice to create a .d.ts file for your project that provides and additional interfaces that you might need/want, or if you want to do that within the modules where you have functional code.