如何使用可选的对象成员?
How to use optional object members?
我有一个总是有两个成员的对象,在某些情况下,可以添加第三个。
尝试#1:可选成员
let initObject = {
headers: headers,
method: method ? 'GET' : method,
body?: ''
}
if (method === 'POST') {
initObject.body = body
}
失败 TS1162: An object member cannot be declared optional.
尝试#2:强行添加成员:
let initObject = {
headers: headers,
method: method ? 'GET' : method,
}
if (method === 'POST') {
initObject.body = body
}
现在失败 TS2339: Property 'body' does not exist on type '{ headers: Headers; method: string; }'.
如何向对象添加可选成员?
我目前使用一种解决方法,但它是重复的,我相信有更好的方法
let initObject
if (method === 'GET') {
initObject = {
headers: headers,
method: 'GET',
}
} else if (method === 'POST') {
initObject = {
headers: headers,
method: 'POST',
body: body
}
}
分别使用一个对象类型 interface(您仍然需要正确设置类型):
Each property in an object type can specify a couple of things: the
type, whether the property is optional, and whether the property can
be written to.
interface InitObject {
headers: any;
method: any; // e.g. 'GET' | 'POST'
body?: any; // body is optional
}
let initObject: InitObject;
...
if (method === 'GET') {
initObject = {
headers: headers,
method: 'GET'
};
} else if (method === 'POST') {
initObject = {
headers: headers,
method: 'POST',
body: body
};
}
编辑: 这也可以通过使用界面来实现:
let initObject: InitObject = {
headers: headers,
method: method ? 'GET' : method
};
if (method === 'POST') { // or maybe initObject.method === 'POST'
initObject.body = body;
}
我认为@pzaenger 的回答是最好的解决方案,但如果您不想使用界面,可以使用:
const initObject = {
headers,
method: method || 'GET',
body: method === 'POST' ? body : undefined
}
此外,您为 method
使用的三元条件对我来说没有意义
我有一个总是有两个成员的对象,在某些情况下,可以添加第三个。
尝试#1:可选成员
let initObject = {
headers: headers,
method: method ? 'GET' : method,
body?: ''
}
if (method === 'POST') {
initObject.body = body
}
失败 TS1162: An object member cannot be declared optional.
尝试#2:强行添加成员:
let initObject = {
headers: headers,
method: method ? 'GET' : method,
}
if (method === 'POST') {
initObject.body = body
}
现在失败 TS2339: Property 'body' does not exist on type '{ headers: Headers; method: string; }'.
如何向对象添加可选成员?
我目前使用一种解决方法,但它是重复的,我相信有更好的方法
let initObject
if (method === 'GET') {
initObject = {
headers: headers,
method: 'GET',
}
} else if (method === 'POST') {
initObject = {
headers: headers,
method: 'POST',
body: body
}
}
分别使用一个对象类型 interface(您仍然需要正确设置类型):
Each property in an object type can specify a couple of things: the type, whether the property is optional, and whether the property can be written to.
interface InitObject {
headers: any;
method: any; // e.g. 'GET' | 'POST'
body?: any; // body is optional
}
let initObject: InitObject;
...
if (method === 'GET') {
initObject = {
headers: headers,
method: 'GET'
};
} else if (method === 'POST') {
initObject = {
headers: headers,
method: 'POST',
body: body
};
}
编辑: 这也可以通过使用界面来实现:
let initObject: InitObject = {
headers: headers,
method: method ? 'GET' : method
};
if (method === 'POST') { // or maybe initObject.method === 'POST'
initObject.body = body;
}
我认为@pzaenger 的回答是最好的解决方案,但如果您不想使用界面,可以使用:
const initObject = {
headers,
method: method || 'GET',
body: method === 'POST' ? body : undefined
}
此外,您为 method
使用的三元条件对我来说没有意义