So I am developing for a couple a devices and basically need to do a check to determine the device. For example
// Android being js interface
if(Android) return setupAndroid()
else if(exampleDevice) return method()
etc
If you have done this before you know my problem. If you open this app in a browser not on android it will crash because android does not exist. I have to do it this way and since ive started I do not want to stop till I figure out a way to handle this. I have tried creating a ts interface and did not work. I need a way to say this Android interface does exist only in the android layer. So this type of check is going to happen for every device. The number of devices does not matter. What matters is how do you avoid errors of non existing variables.
I'm guessing you're exposing a specific Android interface via WebView.addJavascriptInterface, but as far as TypeScript is concerned you should declare the global variable as if it always exists. If you want to be extra safe, mark it as possibly undefined so you always have to check its existence before use.
interface AndroidType {
field1: string;
method1(): void;
}
declare var Android: AndroidType | undefined;
if (Android) {
Android.field1 = "foo";
Android.method1();
}