I'm trying to get the googlemaps typing from https://github.com/borisyankov/DefinitelyTyped/tree/master/googlemaps to enforce types on MapOptions. So given the following bad code:
/// <reference path="google.maps.d.ts" />
var map;
function initialize() {
var mapOptions = {
zoom: "no, not really",
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
I was hoping the MapOptions type from google.maps.d.ts would flag the zoom attribute as incorrect. Why isn't tsc
flagging this as a type error?
Update: tried the following version to include more include more type information for the compiler to chew on, but still no dice:
/// <reference path="google.maps.d.ts" />
var map;
function initialize() {
var mapOptions : google.maps.MapOptions = {
zoom: "no, not really",
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
}
You haven't typed your mapOptions
variable, and there is no way for the compiler to infer it.
Try var mapOptions:google.maps.MapOptions = {
(etc).
The interface definition of zoom?:number
looks good in the d.ts that you reference.
Type inference would work if you created the options as part of the map constructor call:
map = new google.maps.Map(document.getElementById('map-canvas'),
{zoom: 'this-should-fail'});
... because here the compiler knows the signature of the constructor and can infer the type. In your sample, mapOptions
is just an untyped object.
UPDATE In VS2012, with WebEssentials 2012 2.7, and TS 0.8.3.1, I get the following:
...and on hover or in the WebEssentials output panel the error output from TSC is:
Compile Error. See error list for details D:/PROJECT/App/viewmodels/hops/map.ts(147,49): Cannot convert '{ zoom: string; }' to 'google.maps.MapOptions': Types of property 'zoom' of types '{ zoom: string; }' and 'MapOptions' are incompatible
The error comes from the compiler, but I don't know how much of the surfacing is achieved by WebEssentials.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With