【发布时间】:2020-12-27 12:40:05
【问题描述】:
为什么其中一个 sn-ps 给出 TSC 错误而另一个不给出? 它们都产生相同的结果。 nameParts 是一个字符串数组。
if(nameParts[nameParts.length-1]){
//error: Type 'string | undefined' is not assignable to type 'string'.
// Type 'undefined' is not assignable to type 'string'.
const lastName:string = nameParts[nameParts.length-1];
}
const lastWord = nameParts[nameParts.length-1];
if(lastWord){
const lastName:string = lastWord;
}
【问题讨论】:
标签: typescript expression type-inference