为什么 Flowtype 允许将空对象 ({}) 分配给一个类型?
Why does Flowtype allow assigning empty object ({}) to a type?
谁能给我解释一下为什么 a2
没有报错?
type A = {
name: string;
type: number;
};
const a1: A = null; // NOT OK
const a2: A = {}; // OK
const a3: A = { name: "aaa" }; // NOT OK
const a4: A = { name: "aaa", type: 6 }; // OK
我在 Typescript 中尝试了同样的操作,但 a2
无法编译。我同意打字稿。为什么 Flowtype 认为它可以?有什么设置可以让我让它不正常吗?
{}
似乎是一个特殊的东西——未密封的对象(https://flow.org/en/docs/types/objects/#toc-unsealed-objects)。思路是让你一步步填充对象:
type A = {
name: string;
type: number;
};
const a2: A = {};
a2.name = "FOO";
a2.type = 3;
关于它在各种不同情况下究竟应该如何工作,似乎存在很多困惑:
谁能给我解释一下为什么 a2
没有报错?
type A = {
name: string;
type: number;
};
const a1: A = null; // NOT OK
const a2: A = {}; // OK
const a3: A = { name: "aaa" }; // NOT OK
const a4: A = { name: "aaa", type: 6 }; // OK
我在 Typescript 中尝试了同样的操作,但 a2
无法编译。我同意打字稿。为什么 Flowtype 认为它可以?有什么设置可以让我让它不正常吗?
{}
似乎是一个特殊的东西——未密封的对象(https://flow.org/en/docs/types/objects/#toc-unsealed-objects)。思路是让你一步步填充对象:
type A = {
name: string;
type: number;
};
const a2: A = {};
a2.name = "FOO";
a2.type = 3;
关于它在各种不同情况下究竟应该如何工作,似乎存在很多困惑: