我是一个新的流程,目前正在处理一个严格的流程项目。我有一个类型别名被传递给一个类。该代码片段可以在here中找到
// @flow strict
export type CustomType = {
a: string,
b: boolean,
c: boolean,
d: boolean,
e: boolean
};
let d = true;
let b = true;
let customType:CustomType = {
d,
b,
c: true,
}
class Custom{
constructor(customType = {}) {}
}
let custom = new Custom(customType);
在传递对象时,并不是所有属性customType都存在。这里最好的解决方法是什么?
发布于 2018-05-31 20:23:31
您可以将CustomType
对象的键键入为optional
(Try)
// @flow strict
export type CustomType = {
a?: string,
b?: boolean,
c?: boolean,
d?: boolean,
e?: boolean
};
let d = true;
let b = true;
let customType: CustomType = {
d,
b,
c: true,
}
class Custom{
constructor(customType: CustomType = {}) {}
}
let custom = new Custom(customType);
或者,您可以使用$Shape<T>
。它只允许你传递你感兴趣的密钥:
(Try)
// @flow strict
export type CustomType = {
a: string,
b: boolean,
c: boolean,
d: boolean,
e: boolean
};
let d = true;
let b = true;
let customType: $Shape<CustomType> = {
d,
b,
c: true,
}
class Custom{
constructor(customType: $Shape<CustomType> = {}) {}
}
let custom = new Custom(customType);
https://stackoverflow.com/questions/50621164
复制相似问题