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

As part of FW-1265, the `@angular/compiler` 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:28:04 +02:00
committed by Miško Hevery
parent 2200884e55
commit 012b535147
11 changed files with 28 additions and 25 deletions

View File

@ -38,8 +38,8 @@ class _ExecutionContext {
exports: string[] = [];
constructor(
public parent: _ExecutionContext|null, public instance: any, public className: string|null,
public vars: Map<string, any>) {}
public parent: _ExecutionContext|null, public instance: Object|null,
public className: string|null, public vars: Map<string, any>) {}
createChildWihtLocalVars(): _ExecutionContext {
return new _ExecutionContext(this, this.instance, this.className, new Map<string, any>());
@ -79,9 +79,9 @@ function createDynamicClass(
const ctorParamNames = _classStmt.constructorMethod.params.map(param => param.name);
// Note: use `function` instead of arrow function to capture `this`
const ctor = function(...args: any[]) {
const ctor = function(this: Object, ...args: any[]) {
const instanceCtx = new _ExecutionContext(_ctx, this, _classStmt.name, _ctx.vars);
_classStmt.fields.forEach((field) => { this[field.name] = undefined; });
_classStmt.fields.forEach((field) => { (this as any)[field.name] = undefined; });
_executeFunctionStatements(
ctorParamNames, args, _classStmt.constructorMethod.body, instanceCtx, _visitor);
};
@ -125,7 +125,7 @@ class StatementInterpreter implements o.StatementVisitor, o.ExpressionVisitor {
if (ast.builtin != null) {
switch (ast.builtin) {
case o.BuiltinVar.Super:
return ctx.instance.__proto__;
return Object.getPrototypeOf(ctx.instance);
case o.BuiltinVar.This:
return ctx.instance;
case o.BuiltinVar.CatchError:
@ -188,7 +188,7 @@ class StatementInterpreter implements o.StatementVisitor, o.ExpressionVisitor {
const args = this.visitAllExpressions(stmt.args, ctx);
const fnExpr = stmt.fn;
if (fnExpr instanceof o.ReadVarExpr && fnExpr.builtin === o.BuiltinVar.Super) {
ctx.instance.constructor.prototype.constructor.apply(ctx.instance, args);
ctx.instance !.constructor.prototype.constructor.apply(ctx.instance, args);
return null;
} else {
const fn = stmt.fn.visitExpression(this, ctx);