Skip to content

Commit 208f3d4

Browse files
committed
fix(typings): repair broken type-checking for StringMap
Note that the previous type of StringMap was overly permissive and didn't catch errors. Also we have to explicitly type empty objects, which is explained here: microsoft/TypeScript#5089 Closes #4487
1 parent 7c4199c commit 208f3d4

File tree

16 files changed

+36
-36
lines changed

16 files changed

+36
-36
lines changed

modules/angular2/src/core/compiler/directive_metadata.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ export class CompileDirectiveMetadata {
113113
lifecycleHooks?: LifecycleHooks[],
114114
template?: CompileTemplateMetadata
115115
} = {}): CompileDirectiveMetadata {
116-
var hostListeners = {};
117-
var hostProperties = {};
118-
var hostAttributes = {};
116+
var hostListeners: {[key: string]: string} = {};
117+
var hostProperties: {[key: string]: string} = {};
118+
var hostAttributes: {[key: string]: string} = {};
119119
if (isPresent(host)) {
120120
StringMapWrapper.forEach(host, (value: string, key: string) => {
121121
var matches = RegExpWrapper.firstMatch(HOST_REG_EXP, key);
@@ -128,7 +128,7 @@ export class CompileDirectiveMetadata {
128128
}
129129
});
130130
}
131-
var inputsMap = {};
131+
var inputsMap: {[key: string]: string} = {};
132132
if (isPresent(inputs)) {
133133
inputs.forEach((bindConfig: string) => {
134134
// canonical syntax: `dirProp: elProp`
@@ -137,7 +137,7 @@ export class CompileDirectiveMetadata {
137137
inputsMap[parts[0]] = parts[1];
138138
});
139139
}
140-
var outputsMap = {};
140+
var outputsMap: {[key: string]: string} = {};
141141
if (isPresent(outputs)) {
142142
outputs.forEach((bindConfig: string) => {
143143
// canonical syntax: `dirProp: elProp`

modules/angular2/src/core/dom/generic_browser_adapter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export abstract class GenericBrowserDomAdapter extends DomAdapter {
2323
}
2424
}
2525
}
26-
var transEndEventNames = {
26+
var transEndEventNames: {[key: string]: string} = {
2727
WebkitTransition: 'webkitTransitionEnd',
2828
MozTransition: 'transitionend',
2929
OTransition: 'oTransitionEnd otransitionend',

modules/angular2/src/core/dom/parse5_adapter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import {BaseException, WrappedException} from 'angular2/src/core/facade/exceptions';
1818
import {SelectorMatcher, CssSelector} from 'angular2/src/core/compiler/selector';
1919

20-
var _attrToPropMap = {
20+
var _attrToPropMap: {[key: string]: string} = {
2121
'class': 'className',
2222
'innerHtml': 'innerHTML',
2323
'readonly': 'readOnly',

modules/angular2/src/core/facade/collection.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class MapWrapper {
8686
return result;
8787
}
8888
static toStringMap<T>(m: Map<string, T>): {[key: string]: T} {
89-
var r = {};
89+
var r: {[key: string]: T} = {};
9090
m.forEach((v, k) => r[k] = v);
9191
return r;
9292
}
@@ -135,7 +135,7 @@ export class StringMapWrapper {
135135
}
136136

137137
static merge<V>(m1: {[key: string]: V}, m2: {[key: string]: V}): {[key: string]: V} {
138-
var m = {};
138+
var m: {[key: string]: V} = {};
139139

140140
for (var attr in m1) {
141141
if (m1.hasOwnProperty(attr)) {

modules/angular2/src/core/forms/form_builder.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class FormBuilder {
9797
}
9898

9999
_reduceControls(controlsConfig: any): {[key: string]: modelModule.AbstractControl} {
100-
var controls = {};
100+
var controls: {[key: string]: modelModule.AbstractControl} = {};
101101
StringMapWrapper.forEach(controlsConfig, (controlConfig, controlName) => {
102102
controls[controlName] = this._createControl(controlConfig);
103103
});

modules/angular2/src/core/forms/validators.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ export class Validators {
2929
return function(control: modelModule.Control) {
3030
var res = ListWrapper.reduce(validators, (res, validator) => {
3131
var errors = validator(control);
32-
return isPresent(errors) ? StringMapWrapper.merge(res, errors) : res;
32+
return isPresent(errors) ? StringMapWrapper.merge(<any>res, <any>errors) : res;
3333
}, {});
3434
return StringMapWrapper.isEmpty(res) ? null : res;
3535
};
3636
}
3737

38-
static group(group: modelModule.ControlGroup): {[key: string]: boolean} {
39-
var res = {};
38+
static group(group: modelModule.ControlGroup): {[key: string]: any[]} {
39+
var res: {[key: string]: any[]} = {};
4040
StringMapWrapper.forEach(group.controls, (control, name) => {
4141
if (group.contains(name) && isPresent(control.errors)) {
4242
Validators._mergeErrors(control, res);
@@ -45,8 +45,8 @@ export class Validators {
4545
return StringMapWrapper.isEmpty(res) ? null : res;
4646
}
4747

48-
static array(array: modelModule.ControlArray): {[key: string]: boolean} {
49-
var res = {};
48+
static array(array: modelModule.ControlArray): {[key: string]: any[]} {
49+
var res: {[key: string]: any[]} = {};
5050
array.controls.forEach((control) => {
5151
if (isPresent(control.errors)) {
5252
Validators._mergeErrors(control, res);

modules/angular2/src/core/linker/directive_resolver.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export class DirectiveResolver {
4646
propertyMetadata: {[key: string]: any[]}): DirectiveMetadata {
4747
var inputs = [];
4848
var outputs = [];
49-
var host = {};
50-
var queries = {};
49+
var host: {[key: string]: string} = {};
50+
var queries: {[key: string]: any} = {};
5151

5252
StringMapWrapper.forEach(propertyMetadata, (metadata: any[], propName: string) => {
5353
metadata.forEach(a => {

modules/angular2/src/core/linker/proto_view_factory.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ export class ProtoViewFactory {
5555
var result = this._cache.get(compiledTemplate.id);
5656
if (isBlank(result)) {
5757
var templateData = compiledTemplate.getData(this._appId);
58-
result =
59-
new AppProtoView(templateData.commands, ViewType.HOST, true,
60-
templateData.changeDetectorFactory, null, new ProtoPipes(new Map()));
58+
var emptyMap: {[key: string]: PipeBinding} = {};
59+
result = new AppProtoView(templateData.commands, ViewType.HOST, true,
60+
templateData.changeDetectorFactory, null, new ProtoPipes(emptyMap));
6161
this._cache.set(compiledTemplate.id, result);
6262
}
6363
return result;

modules/angular2/src/core/pipes/date_pipe.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ var defaultLocale: string = 'en-US';
8181
@Pipe({name: 'date'})
8282
@Injectable()
8383
export class DatePipe implements PipeTransform {
84-
static _ALIASES = {
84+
static _ALIASES: {[key: string]: String} = {
8585
'medium': 'yMMMdjms',
8686
'short': 'yMdjm',
8787
'fullDate': 'yMMMMEEEEd',

modules/angular2/src/core/pipes/pipes.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import * as cd from 'angular2/src/core/change_detection/pipes';
1414

1515
export class ProtoPipes {
1616
static fromBindings(bindings: PipeBinding[]): ProtoPipes {
17-
var config = {};
17+
var config: {[key: string]: PipeBinding} = {};
1818
bindings.forEach(b => config[b.name] = b);
1919
return new ProtoPipes(config);
2020
}

modules/angular2/src/core/zone/ng_zone.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export class NgZone {
251251
var errorHandling;
252252

253253
if (enableLongStackTrace) {
254-
errorHandling = StringMapWrapper.merge(Zone.longStackTraceZone,
254+
errorHandling = StringMapWrapper.merge(<any>Zone.longStackTraceZone,
255255
{onError: function(e) { ngZone._onError(this, e); }});
256256
} else {
257257
errorHandling = {onError: function(e) { ngZone._onError(this, e); }};

modules/angular2/src/http/headers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class Headers {
4444
if (headers instanceof Headers) {
4545
this._headersMap = (<Headers>headers)._headersMap;
4646
} else /*if (headers instanceof StringMap)*/ {
47-
this._headersMap = MapWrapper.createFromStringMap<string[]>(headers);
47+
this._headersMap = MapWrapper.createFromStringMap<string[]>(<{[key: string]: any}>headers);
4848
MapWrapper.forEach(this._headersMap, (v, k) => {
4949
if (!isListLikeIterable(v)) {
5050
var list = [];

modules/angular2/src/router/route_registry.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export class RouteRegistry {
155155
}
156156

157157
var componentRecognizer = this._rules.get(parentComponent);
158-
var auxInstructions = {};
158+
var auxInstructions: {[key: string]: Instruction} = {};
159159

160160
var promises = instruction.auxUrls.map((auxSegment: Url) => {
161161
var match = componentRecognizer.recognizeAuxiliary(auxSegment);

modules/angular2/test/core/facade/collection_spec.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -109,33 +109,33 @@ export function main() {
109109
() => { expect(StringMapWrapper.equals({}, {})).toBe(true); });
110110

111111
it('should return true when comparing the same map', () => {
112-
var m1 = {'a': 1, 'b': 2, 'c': 3};
112+
var m1: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3};
113113
expect(StringMapWrapper.equals(m1, m1)).toBe(true);
114114
});
115115

116116
it('should return true when comparing different maps with the same keys and values', () => {
117-
var m1 = {'a': 1, 'b': 2, 'c': 3};
118-
var m2 = {'a': 1, 'b': 2, 'c': 3};
117+
var m1: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3};
118+
var m2: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3};
119119
expect(StringMapWrapper.equals(m1, m2)).toBe(true);
120120
});
121121

122122
it('should return false when comparing maps with different numbers of keys', () => {
123-
var m1 = {'a': 1, 'b': 2, 'c': 3};
124-
var m2 = {'a': 1, 'b': 2, 'c': 3, 'd': 4};
123+
var m1: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3};
124+
var m2: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3, 'd': 4};
125125
expect(StringMapWrapper.equals(m1, m2)).toBe(false);
126126
expect(StringMapWrapper.equals(m2, m1)).toBe(false);
127127
});
128128

129129
it('should return false when comparing maps with different keys', () => {
130-
var m1 = {'a': 1, 'b': 2, 'c': 3};
131-
var m2 = {'a': 1, 'b': 2, 'CC': 3};
130+
var m1: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3};
131+
var m2: {[key: string]: number} = {'a': 1, 'b': 2, 'CC': 3};
132132
expect(StringMapWrapper.equals(m1, m2)).toBe(false);
133133
expect(StringMapWrapper.equals(m2, m1)).toBe(false);
134134
});
135135

136136
it('should return false when comparing maps with different values', () => {
137-
var m1 = {'a': 1, 'b': 2, 'c': 3};
138-
var m2 = {'a': 1, 'b': 20, 'c': 3};
137+
var m1: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3};
138+
var m2: {[key: string]: number} = {'a': 1, 'b': 20, 'c': 3};
139139
expect(StringMapWrapper.equals(m1, m2)).toBe(false);
140140
expect(StringMapWrapper.equals(m2, m1)).toBe(false);
141141
});

modules/angular2/test/test_lib/utils_spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export function main() {
187187
}
188188
];
189189

190-
browsers.forEach((browser) => {
190+
browsers.forEach((browser: {[key: string]: any}) => {
191191
it(`should detect ${StringMapWrapper.get(browser, 'name')}`, () => {
192192
var bd = new BrowserDetection(<string>StringMapWrapper.get(browser, 'ua'));
193193
expect(bd.isFirefox).toBe(StringMapWrapper.get(browser, 'isFirefox'));

modules/benchpress/test/metric/multi_metric_spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class MockMetric extends Metric {
7777
}
7878

7979
describe(): {[key: string]: string} {
80-
var result = {};
80+
var result: {[key: string]: string} = {};
8181
result[this._id] = 'describe';
8282
return result;
8383
}

0 commit comments

Comments
 (0)