refactor(angular): enable TS strict

This commit is contained in:
octicon-git-branch(16/)
octicon-tag(16/)
Manu Mtz.-Almeida
2018-07-25 14:29:32 +02:00
gitea-unlock(16/)
parent a5898163b6
commit f1c2c0c1ba
octicon-diff(16/tw-mr-1) 11 changed files with 62 additions and 76 deletions

16
angular/src/providers/angular-delegate.ts
View File

@@ -28,7 +28,7 @@ export class AngularFrameworkDelegate implements FrameworkDelegate {
constructor(
private resolver: ComponentFactoryResolver,
private injector: Injector,
private location: ViewContainerRef,
private location: ViewContainerRef | undefined,
private appRef: ApplicationRef,
private zone: NgZone,
) {}
@@ -62,10 +62,10 @@ export class AngularFrameworkDelegate implements FrameworkDelegate {
export function attachView(
resolver: ComponentFactoryResolver,
injector: Injector,
location: ViewContainerRef|undefined,
location: ViewContainerRef | undefined,
appRef: ApplicationRef,
elRefMap: WeakMap<HTMLElement, any>,
container: any, component: any, params: any, cssClasses: string[]
container: any, component: any, params: any, cssClasses: string[] | undefined
) {
const factory = resolver.resolveComponentFactory(component);
const childInjector = Injector.create(getProviders(params), injector);
@@ -78,8 +78,10 @@ export function attachView(
if (params) {
Object.assign(instance, params);
}
for (const clazz of cssClasses) {
hostElement.classList.add(clazz);
if (cssClasses) {
for (const clazz of cssClasses) {
hostElement.classList.add(clazz);
}
}
bindLifecycleEvents(instance, hostElement);
container.appendChild(hostElement);
@@ -103,8 +105,8 @@ const LIFECYCLES = [
export function bindLifecycleEvents(instance: any, element: HTMLElement) {
LIFECYCLES.forEach(eventName => {
element.addEventListener(eventName, (ev: CustomEvent) => {
if (typeof instance[eventName] === 'function') {
element.addEventListener(eventName, (ev: any) => {
if (typeof instance[eventName] === 'function' && ev.detail) {
instance[eventName](ev.detail);
}
});

6
angular/src/providers/config.ts
View File

@@ -18,7 +18,7 @@ export class Config {
if (c) {
return c.getBoolean(key, fallback);
}
return null;
return false;
}
getNumber(key: string, fallback?: number): number {
@@ -26,7 +26,7 @@ export class Config {
if (c) {
return c.getNumber(key, fallback);
}
return null;
return 0;
}
set(key: string, value?: any) {
@@ -39,7 +39,7 @@ export class Config {
export const ConfigToken = new InjectionToken<any>('USERCONFIG');
function getConfig(): CoreConfig {
function getConfig(): CoreConfig | null {
const win: IonicWindow = window as any;
if (typeof win !== 'undefined') {
const Ionic = win.Ionic;

53
angular/src/providers/events.ts
View File

@@ -3,7 +3,7 @@ import { Injectable } from '@angular/core';
@Injectable()
export class Events {
private c: {[topic: string]: Function[]} = [] as any;
private c = new Map<string, Function[]>();
/**
* Subscribe to an event topic. Events that get posted to that topic will trigger the provided handler.
@@ -12,12 +12,11 @@ export class Events {
* @param {function} handler the event handler
*/
subscribe(topic: string, ...handlers: Function[]) {
if (!this.c[topic]) {
this.c[topic] = [];
let topics = this.c.get(topic);
if (!topics) {
topics = [];
}
handlers.forEach((handler) => {
this.c[topic].push(handler);
});
topics.push(...handlers);
}
/**
@@ -28,34 +27,27 @@ export class Events {
*
* @return true if a handler was removed
*/
unsubscribe(topic: string, handler: Function = null) {
const t = this.c[topic];
if (!t) {
// Wasn't found, wasn't removed
return false;
unsubscribe(topic: string, handler?: Function): boolean {
if (!handler) {
return this.c.delete(topic);
}
if (!handler) {
// Remove all handlers for this topic
delete this.c[topic];
return true;
const topics = this.c.get(topic);
if (!topics) {
return false;
}
// We need to find and remove a specific handler
const i = t.indexOf(handler);
const index = topics.indexOf(handler);
if (i < 0) {
if (index < 0) {
// Wasn't found, wasn't removed
return false;
}
t.splice(i, 1);
// If the channel is empty now, remove it from the channel map
if (!t.length) {
delete this.c[topic];
topics.splice(index, 1);
if (topics.length === 0) {
this.c.delete(topic);
}
return true;
}
@@ -65,17 +57,12 @@ export class Events {
* @param {string} topic the topic to publish to
* @param {any} eventData the data to send as the event
*/
publish(topic: string, ...args: any[]) {
const t = this.c[topic];
if (!t) {
publish(topic: string, ...args: any[]): any[] | null {
const topics = this.c.get(topic);
if (!topics) {
return null;
}
const responses: any[] = [];
t.forEach((handler: any) => {
responses.push(handler(...args));
});
return responses;
return topics.map((handler => handler(...args)));
}
}

2
angular/src/providers/modal-controller.ts
View File

@@ -14,7 +14,7 @@ export class ModalController extends OverlayBaseController<ModalOptions, HTMLIon
super('ion-modal-controller');
}
create(opts?: ModalOptions): Promise<HTMLIonModalElement> {
create(opts: ModalOptions): Promise<HTMLIonModalElement> {
return super.create({
...opts,
delegate: this.angularDelegate.create(this.resolver, this.injector)

6
angular/src/providers/nav-controller.ts
View File

@@ -21,17 +21,17 @@ export class NavController {
goForward(url: string | UrlTree, animated?: boolean, extras?: NavigationExtras) {
this.setIntent(NavIntent.Forward, animated);
return this.router.navigateByUrl(url, extras);
return this.router!.navigateByUrl(url, extras);
}
goBack(url: string | UrlTree, animated?: boolean, extras?: NavigationExtras) {
this.setIntent(NavIntent.Back, animated);
return this.router.navigateByUrl(url, extras);
return this.router!.navigateByUrl(url, extras);
}
goRoot(url: string | UrlTree, animated?: boolean, extras?: NavigationExtras) {
this.setIntent(NavIntent.Root, animated);
return this.router.navigateByUrl(url, extras);
return this.router!.navigateByUrl(url, extras);
}
setIntent(intent: NavIntent, animated?: boolean) {

4
angular/src/providers/platform.ts
View File

@@ -49,7 +49,7 @@ export class Platform {
readyResolve('cordova');
}, {once: true});
} else {
readyResolve('dom');
readyResolve!('dom');
}
}
@@ -169,7 +169,7 @@ export class Platform {
/**
* Get the query string parameter
*/
getQueryParam(key: string): string {
getQueryParam(key: string): string | null {
return readQueryParam(window.location.href, key);
}

2
angular/src/providers/popover-controller.ts
View File

@@ -14,7 +14,7 @@ export class PopoverController extends OverlayBaseController<PopoverOptions, HTM
super('ion-popover-controller');
}
create(opts?: PopoverOptions): Promise<HTMLIonPopoverElement> {
create(opts: PopoverOptions): Promise<HTMLIonPopoverElement> {
return super.create({
...opts,
delegate: this.angularDelegate.create(this.resolver, this.injector)