refactor(core): ensure compatibility with typescript strict flag (#30993)

As part of FW-1265, the `@angular/core` package is made compatible
with the TypeScript `--strict` flag. This already unveiled a few bugs,
so the strictness flag seems to help with increasing the overall code health.

Read more about the strict flag [here](https://www.typescriptlang.org/docs/handbook/compiler-options.html)

PR Close #30993
This commit is contained in:
Paul Gschwendtner
2019-06-14 09:27:41 +02:00
committed by Miško Hevery
parent 78e7fdd98d
commit 2200884e55
29 changed files with 122 additions and 86 deletions

View File

@ -273,7 +273,7 @@ export function ɵɵdefineComponent<T>(componentDefinition: {
pipeDefs: null !, // assigned in noSideEffects
selectors: componentDefinition.selectors,
viewQuery: componentDefinition.viewQuery || null,
features: componentDefinition.features || null,
features: componentDefinition.features as DirectiveDefFeature[] || null,
data: componentDefinition.data || {},
// TODO(misko): convert ViewEncapsulation into const enum so that it can be used directly in the
// next line. Also `None` should be 0 not 2.
@ -324,8 +324,7 @@ export function ɵɵsetComponentScope(
def.pipeDefs = () => pipes.map(extractPipeDef);
}
export function extractDirectiveDef(type: DirectiveType<any>& ComponentType<any>):
DirectiveDef<any>|ComponentDef<any> {
export function extractDirectiveDef(type: Type<any>): DirectiveDef<any>|ComponentDef<any> {
const def = getComponentDef(type) || getDirectiveDef(type);
if (ngDevMode && !def) {
throw new Error(`'${type.name}' is neither 'ComponentType' or 'DirectiveType'.`);
@ -333,7 +332,7 @@ export function extractDirectiveDef(type: DirectiveType<any>& ComponentType<any>
return def !;
}
export function extractPipeDef(type: PipeType<any>): PipeDef<any> {
export function extractPipeDef(type: Type<any>): PipeDef<any> {
const def = getPipeDef(type);
if (ngDevMode && !def) {
throw new Error(`'${type.name}' is not a 'PipeType'.`);

View File

@ -556,7 +556,7 @@ export function getNodeInjectable(
const saveLView = getLView();
setTNodeAndViewData(tNode, lData);
try {
value = lData[index] = factory.factory(null, tData, lData, tNode);
value = lData[index] = factory.factory(undefined, tData, lData, tNode);
} finally {
if (factory.injectImpl) setInjectImplementation(previousInjectImplementation);
setIncludeViewProviders(previousIncludeViewProviders);

View File

@ -204,7 +204,8 @@ function indexOf(item: any, arr: any[], begin: number, end: number) {
* Use this with `multi` `providers`.
*/
function multiProvidersFactoryResolver(
this: NodeInjectorFactory, _: null, tData: TData, lData: LView, tNode: TElementNode): any[] {
this: NodeInjectorFactory, _: undefined, tData: TData, lData: LView,
tNode: TElementNode): any[] {
return multiResolve(this.multi !, []);
}
@ -214,7 +215,8 @@ function multiProvidersFactoryResolver(
* This factory knows how to concatenate itself with the existing `multi` `providers`.
*/
function multiViewProvidersFactoryResolver(
this: NodeInjectorFactory, _: null, tData: TData, lData: LView, tNode: TElementNode): any[] {
this: NodeInjectorFactory, _: undefined, tData: TData, lData: LView,
tNode: TElementNode): any[] {
const factories = this.multi !;
let result: any[];
if (this.providerFactory) {
@ -252,7 +254,8 @@ function multiResolve(factories: Array<() => any>, result: any[]): any[] {
*/
function multiFactory(
factoryFn: (
this: NodeInjectorFactory, _: null, tData: TData, lData: LView, tNode: TElementNode) => any,
this: NodeInjectorFactory, _: undefined, tData: TData, lData: LView, tNode: TElementNode) =>
any,
index: number, isViewProvider: boolean, isComponent: boolean,
f: () => any): NodeInjectorFactory {
const factory = new NodeInjectorFactory(factoryFn, isViewProvider, ɵɵdirectiveInject);

View File

@ -20,7 +20,7 @@ import {diPublicInInjector, getNodeInjectable, getOrCreateNodeInjectorForNode} f
import {throwMultipleComponentError} from '../errors';
import {executeHooks, executePreOrderHooks, registerPreOrderHooks} from '../hooks';
import {ACTIVE_INDEX, CONTAINER_HEADER_OFFSET, LContainer} from '../interfaces/container';
import {ComponentDef, ComponentTemplate, DirectiveDef, DirectiveDefListOrFactory, PipeDefListOrFactory, RenderFlags, ViewQueriesFunction} from '../interfaces/definition';
import {ComponentDef, ComponentTemplate, DirectiveDef, DirectiveDefListOrFactory, FactoryFn, PipeDefListOrFactory, RenderFlags, ViewQueriesFunction} from '../interfaces/definition';
import {INJECTOR_BLOOM_PARENT_SIZE, NodeInjectorFactory} from '../interfaces/injector';
import {AttributeMarker, InitialInputData, InitialInputs, LocalRefExtractor, PropertyAliasValue, PropertyAliases, TAttributes, TContainerNode, TElementContainerNode, TElementNode, TIcuContainerNode, TNode, TNodeFlags, TNodeProviderIndexes, TNodeType, TProjectionNode, TViewNode} from '../interfaces/node';
import {LQueries} from '../interfaces/query';
@ -1288,8 +1288,7 @@ export function initNodeFlags(tNode: TNode, index: number, numberOfDirectives: n
}
function baseResolveDirective<T>(
tView: TView, viewData: LView, def: DirectiveDef<T>,
directiveFactory: (t: Type<T>| null) => any) {
tView: TView, viewData: LView, def: DirectiveDef<T>, directiveFactory: FactoryFn<T>) {
tView.data.push(def);
const nodeInjectorFactory = new NodeInjectorFactory(directiveFactory, isComponentDef(def), null);
tView.blueprint.push(nodeInjectorFactory);

View File

@ -132,7 +132,7 @@ export class NodeInjectorFactory {
/**
* The inject implementation to be activated when using the factory.
*/
injectImpl: null|(<T>(token: Type<T>|InjectionToken<T>, flags: InjectFlags) => T);
injectImpl: null|(<T>(token: Type<T>|InjectionToken<T>, flags?: InjectFlags) => T);
/**
* Marker set to true during factory invocation to see if we get into recursive loop.
@ -216,7 +216,7 @@ export class NodeInjectorFactory {
* Factory to invoke in order to create a new instance.
*/
public factory:
(this: NodeInjectorFactory, _: null,
(this: NodeInjectorFactory, _: undefined,
/**
* array where injectables tokens are stored. This is used in
* case of an error reporting to produce friendlier errors.
@ -234,8 +234,8 @@ export class NodeInjectorFactory {
/**
* Set to `true` if the token is declared in `viewProviders` (or if it is component).
*/
isViewProvider: boolean,
injectImplementation: null|(<T>(token: Type<T>|InjectionToken<T>, flags: InjectFlags) => T)) {
isViewProvider: boolean, injectImplementation: null|
(<T>(token: Type<T>|InjectionToken<T>, flags?: InjectFlags) => T)) {
this.canSeeViewProviders = isViewProvider;
this.injectImpl = injectImplementation;
}

View File

@ -166,7 +166,7 @@ export function ɵɵpipeBind4(
*
* @codeGenApi
*/
export function ɵɵpipeBindV(index: number, slotOffset: number, values: any[]): any {
export function ɵɵpipeBindV(index: number, slotOffset: number, values: [any, ...any[]]): any {
const pipeInstance = ɵɵload<PipeTransform>(index);
return unwrapValue(
isPure(index) ? ɵɵpureFunctionV(slotOffset, pipeInstance.transform, values, pipeInstance) :

View File

@ -61,7 +61,7 @@ function findNextInsertionIndex(buffer: HostInstructionsQueue, priority: number)
* Iterates through the host instructions queue (if present within the provided
* context) and executes each queued instruction entry.
*/
export function flushQueue(context: StylingContext): void {
export function flushQueue(this: unknown, context: StylingContext): void {
const buffer = context[StylingIndex.HostInstructionsQueue];
if (buffer) {
for (let i = HostInstructionsQueueIndex.ValuesStartPosition; i < buffer.length;

View File

@ -201,7 +201,7 @@ export class NodeStylingDebug implements DebugStyling {
return entries;
}
private _mapValues(fn: (prop: string, value: any, bindingIndex: number|null) => any) {
private _mapValues(fn: (prop: string, value: string|null, bindingIndex: number|null) => any) {
// there is no need to store/track an element instance. The
// element is only used when the styling algorithm attempts to
// style the value (and we mock out the stylingApplyFn anyway).
@ -212,9 +212,8 @@ export class NodeStylingDebug implements DebugStyling {
}
const mapFn: ApplyStylingFn =
(renderer: any, element: RElement, prop: string, value: any, bindingIndex: number) => {
fn(prop, value, bindingIndex || null);
};
(renderer: any, element: RElement, prop: string, value: string | null,
bindingIndex?: number | null) => { fn(prop, value, bindingIndex || null); };
const sanitizer = this._isClassBased ? null : (this._sanitizer ||
getCurrentOrLViewSanitizer(this._data as LView));

View File

@ -163,10 +163,10 @@ export function getCurrentOrLViewSanitizer(lView: LView): StyleSanitizeFn|null {
* sanitization.
*/
const sanitizeUsingSanitizerObject: StyleSanitizeFn =
(prop: string, value: string, mode: StyleSanitizeMode) => {
(prop: string, value: string | null, mode?: StyleSanitizeMode) => {
const sanitizer = getCurrentStyleSanitizer() as Sanitizer;
if (sanitizer) {
if (mode & StyleSanitizeMode.SanitizeOnly) {
if (mode !== undefined && mode & StyleSanitizeMode.SanitizeOnly) {
return sanitizer.sanitize(SecurityContext.STYLE, value);
} else {
return true;