You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何为NgTemplateOutlet的let-变量实现基于子组件的类型检查?

如何为NgTemplateOutlet的let-变量实现基于子组件的类型检查?

你遇到的这个情况其实是Angular模板类型检查的一个常见痛点:虽然firstPerson()这类Signal返回的对象能正常触发类型检查(比如访问不存在的属性会立刻报错),但通过内容投影传入子组件的<ng-template #item let-person>里的person变量却没有类型提示和检查,哪怕它的实际类型应该和Signal里的Person完全一致。先看看你遇到的现象:

类型检查差异示意图

问题根源

Angular的模板类型检查器默认没办法自动关联父组件里的内容投影模板和子组件的泛型类型。你的子组件Child<T>虽然声明了itemTemplateTemplateRef<{ $implicit: T }>,但父组件的模板并不知道这个ng-template最终会被子组件用这个类型约束,所以没办法自动推断let-person的类型。

解决方案(满足你不想传递整个数组到指令的要求)

这里有几种优雅的解决方式,不需要额外传递数组到指令:

方法1:显式声明模板的上下文类型

在父组件的类里,给你的ng-template引用变量显式指定TemplateRef的上下文类型,让类型检查器明确知道let-person的类型:

import { Component, computed, signal, ViewChild, TemplateRef } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { Child } from './app/child/child';

@Component({
  selector: 'app-root',
  template: `
    <app-child [items]="people()">
      <ng-template #item let-person>
        {{ person.doesnotexist }} {{ firstPerson().name }}
      </ng-template>
    </app-child>
  `,
  imports: [Child],
})
export class App {
  people = signal<Person[]>([
    {
      name: 'Goldberg',
      age: 27,
    },
    {
      name: 'John Cena',
      age: 29,
    },
  ]);

  firstPerson = computed(() => this.people()[0]);
  // 显式声明item模板的上下文类型
  @ViewChild('item') itemTemplate!: TemplateRef<{ $implicit: Person }>;
}

export interface Person {
  name: string;
  age: number;
}

这样修改后,模板里的let-person就会被正确推断为Person类型,访问person.doesnotexist这类不存在的属性时,类型检查器就会立刻报错。

方法2:用辅助指令实现类型推断(更灵活)

如果你的项目里有多个类似场景,可以创建一个轻量的辅助指令,通过传入一个类型示例来让Angular自动推断模板上下文类型:

// template-type.directive.ts
import { Directive, Input } from '@angular/core';

@Directive({
  selector: '[templateType]'
})
export class TemplateTypeDirective<T> {
  // 只需要传入一个类型示例,不需要实际使用这个值
  @Input() templateType!: T;
}

然后在父组件的模板里使用这个指令:

<app-child [items]="people()">
  <ng-template #item let-person [templateType]="firstPerson()">
    {{ person.doesnotexist }} <!-- 现在会触发类型检查报错 -->
    {{ firstPerson().name }}
  </ng-template>
</app-child>

这里我们传入firstPerson()(类型是Person)作为templateType的值,Angular会自动推断出let-person的类型是Person,而且完全不需要传递整个数组。

方法3:利用子组件的泛型关联(更贴合组件设计)

如果你想让类型完全从子组件的泛型推断而来,可以在父组件里通过ViewChild获取子组件实例,然后从中提取类型:

import { Component, computed, signal, ViewChild } from '@angular/core';
import { Child } from './app/child/child';
import { bootstrapApplication } from '@angular/platform-browser';

@Component({
  selector: 'app-root',
  template: `
    <app-child [items]="people()">
      <ng-template #item let-person>
        {{ person.doesnotexist }} {{ firstPerson().name }}
      </ng-template>
    </app-child>
  `,
  imports: [Child],
})
export class App {
  people = signal<Person[]>([
    {
      name: 'Goldberg',
      age: 27,
    },
    {
      name: 'John Cena',
      age: 29,
    },
  ]);

  firstPerson = computed(() => this.people()[0]);
  // 获取子组件实例,让TypeScript推断其泛型类型
  @ViewChild(Child) childComponent!: Child<Person>;
}

export interface Person {
  name: string;
  age: number;
}

这种方式不需要修改模板,类型检查器会通过子组件的泛型Person,自动关联到内容投影的模板上下文,让let-person获得正确的类型提示。

验证效果

完成上述任意一种修改后,你再访问person.doesnotexist时,Angular的类型检查器就会像处理firstPerson().doesnotexist一样,立刻提示属性不存在的错误,实现和Signal对象一致的类型检查效果。

备注:内容来源于stack exchange,提问作者Sidharth Bajpai

火山引擎 最新活动