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
This commit is contained in:
@ -423,7 +423,7 @@ export class NgtscProgram implements api.Program {
|
||||
if (this.options.fullTemplateTypeCheck) {
|
||||
const strictTemplates = !!this.options.strictTemplates;
|
||||
typeCheckingConfig = {
|
||||
applyTemplateContextGuards: true,
|
||||
applyTemplateContextGuards: strictTemplates,
|
||||
checkQueries: false,
|
||||
checkTemplateBodies: true,
|
||||
checkTypeOfInputBindings: strictTemplates,
|
||||
@ -468,6 +468,7 @@ export class NgtscProgram implements api.Program {
|
||||
// based on "fullTemplateTypeCheck".
|
||||
if (this.options.strictInputTypes !== undefined) {
|
||||
typeCheckingConfig.checkTypeOfInputBindings = this.options.strictInputTypes;
|
||||
typeCheckingConfig.applyTemplateContextGuards = this.options.strictInputTypes;
|
||||
}
|
||||
if (this.options.strictNullInputTypes !== undefined) {
|
||||
typeCheckingConfig.strictNullInputBindings = this.options.strictNullInputTypes;
|
||||
|
Reference in New Issue
Block a user