About declare

hi. I’m meansoup.

I’m learning about Ionic2, but several things hard for me to understand.

today, I want to ask what ‘declare var xxx’ means?

I think it makes global variable, but just that? it doesn’t make any other effect?

when I want to use google map (javascript) and cordova-plugin, I have to do declare var google, declare var navigator.

how it works when i use these code? what it does?

You really want to be using ionic-native instead.

TypeScript uses types so when you use someting, google in your case.
The TypeScript compiler doesnt know what google is.
This often happens when using JS things inside a TypeScript environment. Using declare is kind of a hacky thing where you tell TypeScript you do know this thing called google.
Then when TypeScript gets transpiled into plain JS and youve include your package or script from google. It will reference the right object. But TypScript cant work with unknown “types” without some assistance.

For a lot of stuff you can use Ionic Native, like google maps
But otherwise for some JS packages you can find typings, these create a module that maps to the functions of what you want to use

This is quick explination, someone else might be able to correct or improve on this.

1 Like