如下,已经统一写了个函数判断属性是否存在,不存在会抛出错误,但是ts不认,请问有什么方法可以解决这个问题?
class { prop1?: sring clear() { this._check() this.prop1.split('') // 这里会提示this.prop1可能不存在 } _check() { if (!this.prop1) { throw new Error('请先传入正确的参数') } } }
非空断言 this.prop1!.split('') 应该没错
this.prop1!.split('')
2.1m questions
2.1m answers
60 comments
56.6k users