Skip to content

Commit eb6975a

Browse files
committed
fix(ivy): don't infer template context types when in full mode (#33537)
The Ivy template type-checker is capable of inferring the type of a structural directive (such as NgForOf<T>). Previously, this was done with fullTemplateTypeCheck: true, even if strictTemplates was false. View Engine previously did not do this inference, and so this causes breakages if the type of the template context is not what the user expected. In particular, consider the template: ```html <div *ngFor="let user of users as all"> {{user.index}} out of {{all.length}} </div> ``` As long as `users` is an array, this seems reasonable, because it appears that `all` is an alias for the `users` array. However, this is misleading. In reality, `NgForOf` is rendered with a template context that contains both a `$implicit` value (for the loop variable `user`) as well as a `ngForOf` value, which is the actual value assigned to `all`. The type of `NgForOf`'s template context is `NgForContext<T>`, which declares `ngForOf`'s type to be `NgIterable<T>`, which does not have a `length` property (due to its incorporation of the `Iterable` type). This commit stops the template type-checker from inferring template context types unless strictTemplates is set (and strictInputTypes is not disabled). Fixes #33527. PR Close #33537
1 parent 97fbdab commit eb6975a

2 files changed

Lines changed: 52 additions & 23 deletions

File tree

‎packages/compiler-cli/src/ngtsc/program.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ export class NgtscProgram implements api.Program {
423423
if (this.options.fullTemplateTypeCheck) {
424424
const strictTemplates = !!this.options.strictTemplates;
425425
typeCheckingConfig = {
426-
applyTemplateContextGuards: true,
426+
applyTemplateContextGuards: strictTemplates,
427427
checkQueries: false,
428428
checkTemplateBodies: true,
429429
checkTypeOfInputBindings: strictTemplates,
@@ -468,6 +468,7 @@ export class NgtscProgram implements api.Program {
468468
// based on "fullTemplateTypeCheck".
469469
if (this.options.strictInputTypes !== undefined) {
470470
typeCheckingConfig.checkTypeOfInputBindings = this.options.strictInputTypes;
471+
typeCheckingConfig.applyTemplateContextGuards = this.options.strictInputTypes;
471472
}
472473
if (this.options.strictNullInputTypes !== undefined) {
473474
typeCheckingConfig.strictNullInputBindings = this.options.strictNullInputTypes;

‎packages/compiler-cli/test/ngtsc/template_typecheck_spec.ts‎

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ export declare class IndexPipe {
4343
static ɵpipe: i0.ɵPipeDefWithMeta<IndexPipe, 'index'>;
4444
}
4545
46+
export declare class SlicePipe {
47+
transform<T>(value: ReadonlyArray<T>, start: number, end?: number): Array<T>;
48+
transform(value: string, start: number, end?: number): string;
49+
transform(value: null, start: number, end?: number): null;
50+
transform(value: undefined, start: number, end?: number): undefined;
51+
transform(value: any, start: number, end?: number): any;
52+
53+
static ɵpipe: i0.ɵPipeDefWithMeta<SlicePipe, 'slice'>;
54+
}
55+
4656
export declare class NgForOf<T> {
4757
ngForOf: i0.NgIterable<T>;
4858
static ngTemplateContextGuard<T>(dir: NgForOf<T>, ctx: any): ctx is NgForOfContext<T>;
@@ -56,7 +66,7 @@ export declare class NgIf {
5666
}
5767
5868
export declare class CommonModule {
59-
static ɵmod: i0.ɵɵNgModuleDefWithMeta<CommonModule, [typeof NgIf, typeof NgForOf, typeof IndexPipe], never, [typeof NgIf, typeof NgForOf, typeof IndexPipe]>;
69+
static ɵmod: i0.ɵɵNgModuleDefWithMeta<CommonModule, [typeof NgIf, typeof NgForOf, typeof IndexPipe, typeof SlicePipe], never, [typeof NgIf, typeof NgForOf, typeof IndexPipe, typeof SlicePipe]>;
6070
}
6171
`);
6272
env.write('node_modules/@angular/animations/index.d.ts', `
@@ -862,30 +872,48 @@ export declare class AnimationEvent {
862872
expect(diags[0].length).toBe(19);
863873
});
864874

865-
it('should property type-check a microsyntax variable with the same name as the expression',
866-
() => {
867-
env.write('test.ts', `
868-
import {CommonModule} from '@angular/common';
869-
import {Component, Input, NgModule} from '@angular/core';
875+
describe('microsyntax variables', () => {
876+
beforeEach(() => {
877+
// Use the same template for both tests
878+
env.write('test.ts', `
879+
import {CommonModule} from '@angular/common';
880+
import {Component, NgModule} from '@angular/core';
881+
882+
@Component({
883+
selector: 'test',
884+
template: \`<div *ngFor="let foo of foos as foos">
885+
{{foo.name}} of {{foos.length}}
886+
</div>
887+
\`,
888+
})
889+
export class TestCmp {
890+
foos: {name: string}[];
891+
}
892+
893+
@NgModule({
894+
declarations: [TestCmp],
895+
imports: [CommonModule],
896+
})
897+
export class Module {}
898+
`);
899+
});
870900

871-
@Component({
872-
selector: 'test',
873-
template: '<div *ngIf="foo as foo">{{foo}}</div>',
874-
})
875-
export class TestCmp<T extends {name: string}> {
876-
foo: any;
877-
}
901+
it('should be treated as \'any\' without strictTemplates', () => {
902+
env.tsconfig({fullTemplateTypeCheck: true, strictTemplates: false});
878903

879-
@NgModule({
880-
declarations: [TestCmp],
881-
imports: [CommonModule],
882-
})
883-
export class Module {}
884-
`);
904+
const diags = env.driveDiagnostics();
905+
expect(diags.length).toBe(0);
906+
});
885907

886-
const diags = env.driveDiagnostics();
887-
expect(diags.length).toBe(0);
888-
});
908+
it('should be correctly inferred under strictTemplates', () => {
909+
env.tsconfig({fullTemplateTypeCheck: true, strictTemplates: true});
910+
911+
const diags = env.driveDiagnostics();
912+
expect(diags.length).toBe(1);
913+
expect((diags[0].messageText as ts.DiagnosticMessageChain).messageText)
914+
.toBe(`Property 'length' does not exist on type 'NgIterable<{ name: string; }>'.`);
915+
});
916+
});
889917

890918
it('should properly type-check inherited directives', () => {
891919
env.tsconfig({fullTemplateTypeCheck: true, strictInputTypes: true});

0 commit comments

Comments
 (0)