Angular Ivy类型检查:类型'SafeHtml'无法分配给类型'string'

时间:2019-10-07 08:01:05

标签: angular typescript angular-ivy

切换到 Ivy编译器后,出现 Typescript错误

[Step 4/5] src/app/app.component.html(1,26): Type 'SafeHtml' is not assignable to type 'string'.

在Angular类中,有一个成员属性声明为SafeHtml

@Component({
  selector: 'app',
  template: `<div [innerHTML]="description"></div>`
})
export class AppComponent {
  description: SafeHtml;

  constructor(private sanitizer: DomSanitizer) {}

  ngOnInit(): void {
    this.description = this.sanitizer.sanitize(SecurityContext.HTML, '<strong>whatever comes from server</strong>');
  }
}

我的问题是如何将SafeHtmlSafeUrl转换为字符串?只是.toString()好吗?

角度SafeHtml声明为:

/**
 * Marker interface for a value that's safe to use as HTML.
 *
 * @publicApi
 */
export declare interface SafeHtml extends SafeValue {
}

并且在lib.dom.d.ts中定义了[innerHtml]:

interface InnerHTML {
    innerHTML: string;
}

innerHtml的类型为string,而我有SafeHtml

如何复制:

git clone https://github.com/felikf/angular-repro-safe-html.git
npm i
npm run build

enter image description here

1 个答案:

答案 0 :(得分:1)

这似乎是一个错误,目前,答案是:https://github.com/angular/angular/issues/33028