使用Typescript检查null或未定义

时间:2019-05-16 12:23:47

标签: angular typescript angular7

使用打字稿,我有以下内容:

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会被记录,即使未定义

0 个答案:

没有答案