feat(build): require parameter types

Fixes #2833
This commit is contained in:
Alex Eagle
2015-07-07 20:03:00 -07:00
parent 6d760666a9
commit de18da2a0d
81 changed files with 379 additions and 290 deletions

View File

@ -14,9 +14,9 @@ export interface PromiseCompleter<R> {
}
export class PromiseWrapper {
static resolve(obj): Promise<any> { return Promise.resolve(obj); }
static resolve<T>(obj: T): Promise<T> { return Promise.resolve(obj); }
static reject(obj, _): Promise<any> { return Promise.reject(obj); }
static reject(obj: any, _): Promise<any> { return Promise.reject(obj); }
// Note: We can't rename this method into `catch`, as this is not a valid
// method name in Dart.
@ -29,8 +29,8 @@ export class PromiseWrapper {
return Promise.all(promises);
}
static then<T>(promise: Promise<T>, success: (value: any) => T | Thenable<T>,
rejection?: (error: any, stack?: any) => T | Thenable<T>): Promise<T> {
static then<T, U>(promise: Promise<T>, success: (value: T) => U | Thenable<U>,
rejection?: (error: any, stack?: any) => U | Thenable<U>): Promise<U> {
return promise.then(success, rejection);
}
@ -66,7 +66,9 @@ export class TimerWrapper {
}
export class ObservableWrapper {
static subscribe(emitter: Observable, onNext, onThrow = null, onReturn = null): Object {
static subscribe<T>(emitter: Observable, onNext: (value: T) => void,
onThrow: (exception: any) => void = null,
onReturn: () => void = null): Object {
return emitter.observer({next: onNext, throw: onThrow, return: onReturn});
}
@ -109,7 +111,7 @@ export class EventEmitter extends Observable {
}
}
observer(generator): Rx.IDisposable {
observer(generator: any): Rx.IDisposable {
return this._subject.observeOn(this._immediateScheduler)
.subscribe((value) => { setTimeout(() => generator.next(value)); },
(error) => generator.throw ? generator.throw(error) : null,
@ -118,9 +120,9 @@ export class EventEmitter extends Observable {
toRx(): Rx.Observable<any> { return this._subject; }
next(value) { this._subject.onNext(value); }
next(value: any) { this._subject.onNext(value); }
throw(error) { this._subject.onError(error); }
throw(error: any) { this._subject.onError(error); }
return (value?) { this._subject.onCompleted(); }
return (value?: any) { this._subject.onCompleted(); }
}

View File

@ -55,7 +55,7 @@ var _clearValues: {(m: Map<any, any>)} = (function() {
export class MapWrapper {
static clone<K, V>(m: Map<K, V>): Map<K, V> { return createMapFromMap(m); }
static createFromStringMap(stringMap): Map<string, any> {
static createFromStringMap<T>(stringMap: StringMap<string, T>): Map<string, T> {
var result = new Map();
for (var prop in stringMap) {
result.set(prop, stringMap[prop]);
@ -146,13 +146,13 @@ export interface Predicate<T> { (value: T, index?: number, array?: T[]): boolean
export class ListWrapper {
// JS has no way to express a staticly fixed size list, but dart does so we
// keep both methods.
static createFixedSize(size): List<any> { return new List(size); }
static createGrowableSize(size): List<any> { return new List(size); }
static get(m, k): any { return m[k]; }
static set(m, k, v) { m[k] = v; }
static createFixedSize(size: number): List<any> { return new List(size); }
static createGrowableSize(size: number): List<any> { return new List(size); }
static get<T>(m: List<T>, k: number): T { return m[k]; }
static set<T>(m: List<T>, k: number, v: T) { m[k] = v; }
static clone<T>(array: List<T>): T[] { return array.slice(0); }
static map(array, fn): any { return array.map(fn); }
static forEach(array: List<any>, fn: Function) {
static map<T, V>(array: List<T>, fn: (T) => V): List<V> { return array.map(fn); }
static forEach<T>(array: List<T>, fn: (T) => void) {
for (var i = 0; i < array.length; i++) {
fn(array[i]);
}
@ -171,7 +171,7 @@ export class ListWrapper {
}
return null;
}
static indexOf(array: List<any>, value, startIndex = 0): number {
static indexOf<T>(array: List<T>, value: T, startIndex: number = 0): number {
return array.indexOf(value, startIndex);
}
static reduce<T, E>(list: List<T>,
@ -186,26 +186,26 @@ export class ListWrapper {
}
return false;
}
static contains(list: List<any>, el): boolean { return list.indexOf(el) !== -1; }
static contains<T>(list: List<T>, el: T): boolean { return list.indexOf(el) !== -1; }
static reversed<T>(array: List<T>): T[] {
var a = ListWrapper.clone(array);
return a.reverse();
}
static concat(a, b): List<any> { return a.concat(b); }
static insert(list, index: int, value) { list.splice(index, 0, value); }
static removeAt<T>(list: List<T>, index: int): T {
static concat(a: List<any>, b: List<any>): List<any> { return a.concat(b); }
static insert<T>(list: List<T>, index: number, value: T) { list.splice(index, 0, value); }
static removeAt<T>(list: List<T>, index: number): T {
var res = list[index];
list.splice(index, 1);
return res;
}
static removeAll(list, items) {
static removeAll<T>(list: List<T>, items: List<T>) {
for (var i = 0; i < items.length; ++i) {
var index = list.indexOf(items[i]);
list.splice(index, 1);
}
}
static removeLast<T>(list: List<T>): T { return list.pop(); }
static remove(list, el): boolean {
static remove<T>(list: List<T>, el: T): boolean {
var index = list.indexOf(el);
if (index > -1) {
list.splice(index, 1);
@ -213,10 +213,10 @@ export class ListWrapper {
}
return false;
}
static clear(list) { list.splice(0, list.length); }
static join(list, s: string): string { return list.join(s); }
static isEmpty(list): boolean { return list.length == 0; }
static fill(list: List<any>, value, start: int = 0, end: int = null) {
static clear(list: List<any>) { list.splice(0, list.length); }
static join(list: List<any>, s: string): string { return list.join(s); }
static isEmpty(list: List<any>): boolean { return list.length == 0; }
static fill(list: List<any>, value: any, start: number = 0, end: number = null) {
list.fill(value, start, end === null ? undefined : end);
}
static equals(a: List<any>, b: List<any>): boolean {
@ -226,10 +226,12 @@ export class ListWrapper {
}
return true;
}
static slice<T>(l: List<T>, from: int = 0, to: int = null): List<T> {
static slice<T>(l: List<T>, from: number = 0, to: number = null): List<T> {
return l.slice(from, to === null ? undefined : to);
}
static splice<T>(l: List<T>, from: int, length: int): List<T> { return l.splice(from, length); }
static splice<T>(l: List<T>, from: number, length: number): List<T> {
return l.splice(from, length);
}
static sort<T>(l: List<T>, compareFn?: (a: T, b: T) => number) {
if (isPresent(compareFn)) {
l.sort(compareFn);
@ -241,14 +243,14 @@ export class ListWrapper {
static toJSON<T>(l: List<T>): string { return JSON.stringify(l); }
}
export function isListLikeIterable(obj): boolean {
export function isListLikeIterable(obj: any): boolean {
if (!isJsObject(obj)) return false;
return isArray(obj) ||
(!(obj instanceof Map) && // JS Map are iterables but return entries as [k, v]
Symbol.iterator in obj); // JS Iterable have a Symbol.iterator prop
}
export function iterateListLike(obj, fn: Function) {
export function iterateListLike(obj: any, fn: Function) {
if (isArray(obj)) {
for (var i = 0; i < obj.length; i++) {
fn(obj[i]);

View File

@ -65,35 +65,35 @@ export function IMPLEMENTS(_):<T>(target: T) => T {
return (t) => t;
}
export function isPresent(obj): boolean {
export function isPresent(obj: any): boolean {
return obj !== undefined && obj !== null;
}
export function isBlank(obj): boolean {
export function isBlank(obj: any): boolean {
return obj === undefined || obj === null;
}
export function isString(obj): boolean {
export function isString(obj: any): boolean {
return typeof obj === "string";
}
export function isFunction(obj): boolean {
export function isFunction(obj: any): boolean {
return typeof obj === "function";
}
export function isType(obj): boolean {
export function isType(obj: any): boolean {
return isFunction(obj);
}
export function isStringMap(obj): boolean {
export function isStringMap(obj: any): boolean {
return typeof obj === 'object' && obj !== null;
}
export function isPromise(obj): boolean {
export function isPromise(obj: any): boolean {
return obj instanceof (<any>_global).Promise;
}
export function isArray(obj): boolean {
export function isArray(obj: any): boolean {
return Array.isArray(obj);
}
@ -126,7 +126,7 @@ export class StringWrapper {
static charCodeAt(s: string, index: int): number { return s.charCodeAt(index); }
static split(s: string, regExp): List<string> { return s.split(regExp); }
static split(s: string, regExp: RegExp): List<string> { return s.split(regExp); }
static equals(s: string, s2: string): boolean { return s === s2; }
@ -190,7 +190,7 @@ export class NumberParseError extends BaseException {
export class NumberWrapper {
static toFixed(n: number, fractionDigits: int): string { return n.toFixed(fractionDigits); }
static equal(a, b): boolean { return a === b; }
static equal(a: number, b: number): boolean { return a === b; }
static parseIntAutoRadix(text: string): int {
var result: int = parseInt(text);
@ -224,15 +224,15 @@ export class NumberWrapper {
static get NaN(): number { return NaN; }
static isNaN(value): boolean { return isNaN(value); }
static isNaN(value: any): boolean { return isNaN(value); }
static isInteger(value): boolean { return Number.isInteger(value); }
static isInteger(value: any): boolean { return Number.isInteger(value); }
}
export var RegExp = _global.RegExp;
export class RegExpWrapper {
static create(regExpStr, flags: string = ''): RegExp {
static create(regExpStr: string, flags: string = ''): RegExp {
flags = flags.replace(/g/g, '');
return new _global.RegExp(regExpStr, flags + 'g');
}
@ -242,7 +242,7 @@ export class RegExpWrapper {
return regExp.exec(input);
}
static test(regExp: RegExp, input: string): boolean { return regExp.test(input); }
static matcher(regExp, input): {
static matcher(regExp: RegExp, input: string): {
re: RegExp;
input: string
}
@ -256,11 +256,16 @@ export class RegExpWrapper {
}
export class RegExpMatcherWrapper {
static next(matcher): string { return matcher.re.exec(matcher.input); }
static next(matcher: {
re: RegExp;
input: string
}): string[] {
return matcher.re.exec(matcher.input);
}
}
export class FunctionWrapper {
static apply(fn: Function, posArgs): any { return fn.apply(null, posArgs); }
static apply(fn: Function, posArgs: any): any { return fn.apply(null, posArgs); }
}
// JS has NaN !== NaN
@ -270,11 +275,11 @@ export function looseIdentical(a, b): boolean {
// JS considers NaN is the same as NaN for map Key (while NaN !== NaN otherwise)
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
export function getMapKey(value): any {
export function getMapKey<T>(value: T): T {
return value;
}
export function normalizeBlank(obj): any {
export function normalizeBlank(obj: Object): any {
return isBlank(obj) ? null : obj;
}
@ -282,12 +287,12 @@ export function normalizeBool(obj: boolean): boolean {
return isBlank(obj) ? false : obj;
}
export function isJsObject(o): boolean {
export function isJsObject(o: any): boolean {
return o !== null && (typeof o === "function" || typeof o === "object");
}
export function print(obj) {
if (obj instanceof Error) {
export function print(obj: Error | Object) {
if (obj instanceof BaseException) {
console.log(obj.stack);
} else {
console.log(obj);
@ -297,7 +302,7 @@ export function print(obj) {
// Can't be all uppercase as our transpiler would think it is a special directive...
export class Json {
static parse(s: string): Object { return _global.JSON.parse(s); }
static stringify(data): string {
static stringify(data: Object): string {
// Dart doesn't take 3 arguments
return _global.JSON.stringify(data, null, 2);
}