使用打字稿,我有以下内容:
const data1: Request = { userId: 1, include: 'address' };
const data2 = { userId: 1, include: data1.include, limit: String(data1.limit) };
for (const [key, value] of Object.entries(data2)) {
if (value) {
console.log("key: " + key + ", value: " + value);
}
}
Request
在哪里:
export interface Request {
userId: number;
include?: string;
limit?: number;
}
console.log
的输出是:
key: userId, value: 1
key: include, value: address
key: limit, value: undefined
为什么limit
会被记录,即使未定义