From 04997c84189b01425ddc3093986e9a9c80c31456 Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Wed, 20 Sep 2017 16:26:49 -0700 Subject: [PATCH] =?UTF-8?q?fix(compiler):=20don=E2=80=99t=20type=20check?= =?UTF-8?q?=20property=20access=20of=20literal=20maps=20(#19301)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This was the behavior in Ng4, so we put the stricter check behind the `fullTemplateTypeCheck` flag. PR Close #19301 --- .../test/diagnostics/check_types_spec.ts | 6 +++ .../src/view_compiler/type_check_compiler.ts | 41 +++++++++++-------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/packages/compiler-cli/test/diagnostics/check_types_spec.ts b/packages/compiler-cli/test/diagnostics/check_types_spec.ts index 8acb3604df..7d26284af0 100644 --- a/packages/compiler-cli/test/diagnostics/check_types_spec.ts +++ b/packages/compiler-cli/test/diagnostics/check_types_spec.ts @@ -141,6 +141,12 @@ describe('ng type checker', () => { '
{{"hello" | aPipe}}
', `Argument of type '"hello"' is not assignable to parameter of type 'number'.`, '0:5'); }); + it('should report an index into a map expression', () => { + rejectOnlyWithFullTemplateTypeCheck( + '
{{ {a: 1}[name] }}
', + `Element implicitly has an 'any' type because type '{ a: number; }' has no index signature.`, + '0:5'); + }); it('should report an invalid property on an exportAs directive', () => { rejectOnlyWithFullTemplateTypeCheck( '
{{aDir.fname}}
', diff --git a/packages/compiler/src/view_compiler/type_check_compiler.ts b/packages/compiler/src/view_compiler/type_check_compiler.ts index efa35e969f..9e5f58c6f3 100644 --- a/packages/compiler/src/view_compiler/type_check_compiler.ts +++ b/packages/compiler/src/view_compiler/type_check_compiler.ts @@ -251,26 +251,31 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { context: expression.context, value: convertPropertyBindingBuiltins( { - createLiteralArrayConverter: (argCount: number) => (args: o.Expression[]) => - o.literalArr(args), - createLiteralMapConverter: (keys: {key: string, quoted: boolean}[]) => - (values: o.Expression[]) => { - const entries = keys.map((k, i) => ({ - key: k.key, - value: values[i], - quoted: k.quoted, - })); - return o.literalMap(entries); - }, + createLiteralArrayConverter: (argCount: number) => (args: o.Expression[]) => { + const arr = o.literalArr(args); + // Note: The old view compiler used to use an `any` type + // for arrays. + return this.options.fullTemplateTypeCheck ? arr : arr.cast(o.DYNAMIC_TYPE); + }, + createLiteralMapConverter: + (keys: {key: string, quoted: boolean}[]) => (values: o.Expression[]) => { + const entries = keys.map((k, i) => ({ + key: k.key, + value: values[i], + quoted: k.quoted, + })); + const map = o.literalMap(entries); + // Note: The old view compiler used to use an `any` type + // for maps. + return this.options.fullTemplateTypeCheck ? map : map.cast(o.DYNAMIC_TYPE); + }, createPipeConverter: (name: string, argCount: number) => (args: o.Expression[]) => { // Note: The old view compiler used to use an `any` type - // for pipe calls. - // We keep this behaivor behind a flag for now. - if (this.options.fullTemplateTypeCheck) { - return o.variable(this.pipeOutputVar(name)).callMethod('transform', args); - } else { - return o.variable(this.getOutputVar(o.BuiltinTypeName.Dynamic)); - } + // for pipes. + const pipeExpr = this.options.fullTemplateTypeCheck ? + o.variable(this.pipeOutputVar(name)) : + o.variable(this.getOutputVar(o.BuiltinTypeName.Dynamic)); + return pipeExpr.callMethod('transform', args); }, }, expression.value)