【发布时间】:2021-11-28 20:59:51
【问题描述】:
这是我的课程代码,我收到以下错误:
(1)。 Type 'number' is not assignable to type 'UserId'. - 无法理解这个问题。请需要澄清。
component.ts:
import { Component } from "@angular/core";
interface Identical<T extends number | string> {
id: T;
}
interface UserId {
id: number;
}
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent implements Identical<UserId> {
id: UserId;
title = "CodeSandbox";
constructor(id: number) {
this.id = id;
}
}
就我而言,(2) 我需要将id 传递给班级。处理这个问题的最佳做法是什么?
【问题讨论】:
-
interface Identical不接受UserId类型,所以你可以做interface Identical<T extends number | string | UserId>或只是AppComponent implements Identical<number>
标签: angular typescript typescript-typings typescript-generics