Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix: add better Type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery committed Dec 19, 2016
1 parent 50b168a commit 610649b
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,19 +614,21 @@ const Zone: ZoneType = (function(global: any) {
return this._zoneDelegate.fork(this, zoneSpec);
}

public wrap(callback: Function, source: string): Function {
public wrap<T extends Function>(callback: T, source: string): T {
if (typeof callback !== 'function') {
throw new Error('Expecting function got: ' + callback);
}
const _callback = this._zoneDelegate.intercept(this, callback, source);
const zone: Zone = this;
return function() {
return zone.runGuarded(_callback, this, <any>arguments, source);
};
} as any as T;
}

public run(
callback: Function, applyThis: any = null, applyArgs: any[] = null, source: string = null) {
public run(callback: Function, applyThis?: any, applyArgs?: any[], source?: string): any;
public run<T>(
callback: (...args: any[]) => T, applyThis: any = null, applyArgs: any[] = null,
source: string = null): T {
_currentZoneFrame = new ZoneFrame(_currentZoneFrame, this);
try {
return this._zoneDelegate.invoke(this, callback, applyThis, applyArgs, source);
Expand All @@ -635,8 +637,10 @@ const Zone: ZoneType = (function(global: any) {
}
}

public runGuarded(
callback: Function, applyThis: any = null, applyArgs: any[] = null, source: string = null) {
public runGuarded(callback: Function, applyThis?: any, applyArgs?: any[], source?: string): any;
public runGuarded<T>(
callback: (...args: any[]) => T, applyThis: any = null, applyArgs: any[] = null,
source: string = null) {
_currentZoneFrame = new ZoneFrame(_currentZoneFrame, this);
try {
try {
Expand All @@ -652,7 +656,7 @@ const Zone: ZoneType = (function(global: any) {
}


runTask(task: Task, applyThis?: any, applyArgs?: any) {
runTask(task: Task, applyThis?: any, applyArgs?: any): any {
task.runCount++;
if (task.zone != this)
throw new Error(
Expand Down

0 comments on commit 610649b

Please sign in to comment.