我试图将非泛型映射分配给泛型映射,但是flow抱怨该值不兼容。不管怎么说这件事。在下面的示例中查看m4和m5。
interface Person {
name: string;
}
type Doctor = {
name: string,
license: string,
}
var d:Doctor = {
name: 'Sam',
license: 'PHD'
};
var p: Person = d;
// It is possible to create a generic array where each element
// implements the interface Person
const a: Array<Person> = [d];
// As a Map, it appears you cannot the value cannot be generic array
let m2: Map<string, Array<Doctor>> = new Map<string, Array<Doctor>> ();
let m3: Map<string, Array<Person>> = m2;
// As a Map, it appears that value cannot be a generic object
let m4: Map<string, Doctor> = new Map<string, Doctor> ();
m4.set('bob', d);
let m5: Map<string, Person> = m4;以下语句出错
28: let m5: Map<string, Person> = m4;
^ Cannot assign `m4` to `m5` because property `license` is missing in `Person` [1] but exists in `Doctor` [2] in type argument `V` [3]. [prop-missing]发布于 2021-02-17 00:42:05
这是失败的,因为这样做是有效的
m5.set("foo", { name: "foo" });因为这是一个有效的Person,这将破坏m4,因为它不再包含Doctor对象。
为了使您的代码工作,您的m5需要是只读的,而m3则需要对只读数组进行只读。
let m3: $ReadOnlyMap<string, $ReadOnlyArray<Person>> = m2;和
let m5: $ReadOnlyMap<string, Person> = m4;(流试)
https://stackoverflow.com/questions/66234153
复制相似问题