typescript 学习笔记 - error TS2531: Object is possibly 'null'.

在ts爬虫代码练习时,用cheerio来获取网页中数据时

const viewCountTemp = $(e).find('.postDesc').eq(idx).find('.post-view-count').text() // "阅读(5)"
infoItem.viewCount = parseInt(viewCountTemp.match(/.*\((.*)\)/)[1]) // 5 => "阅读(5)".match(/.*\((.*)\)/)  =>  ["阅读(5)", "5", index: 0, input: "阅读(5)", groups: undefined]

这段代码是报错的。

viewCountTemp.match(/.*\((.*)\)/)
error TS2531: Object is possibly 'null'.
解决方案: 在这个对象后面加一个感叹号 ! ==> 是not null 的断言操作符,不执行运行时检查,告诉编译器只需要知道这个东西
infoItem.viewCount =parseInt(viewCountTemp.match(/.*\((.*)\)/)![1])