admin管理员组文章数量:1516870
Have this code
type IUnionType = IMulti | ISingle;
function isMulti(q: IUnionType): q is IMulti {
return 'ans' in q;
}
//this code is no errors
if (isMulti(o.q[0])) {
o.q[0].ans= []; // this case TS know that o.q[0] is IMulti
}
//this code have error Property 'ans' does not exist on type 'IUnionType'.
Property 'ans' does not exist on type 'ISingle'
var z = 1;
if (isMulti(o.q[z])) {
o.q[z].ans= []; // this case TS DONOT KNOW that o.q[0] is IMulti
}
Difference between code is that i access o.q by index in var z.
Why is have TS error in second case? same error i have if i iterate o.q in for loop
Have this code
type IUnionType = IMulti | ISingle;
function isMulti(q: IUnionType): q is IMulti {
return 'ans' in q;
}
//this code is no errors
if (isMulti(o.q[0])) {
o.q[0].ans= []; // this case TS know that o.q[0] is IMulti
}
//this code have error Property 'ans' does not exist on type 'IUnionType'.
Property 'ans' does not exist on type 'ISingle'
var z = 1;
if (isMulti(o.q[z])) {
o.q[z].ans= []; // this case TS DONOT KNOW that o.q[0] is IMulti
}
Difference between code is that i access o.q by index in var z.
Why is have TS error in second case? same error i have if i iterate o.q in for loop
Share Improve this question asked yesterday Andrew MAndrew M 173 bronze badges 5 |1 Answer
Reset to default 0TS doesn't support scoping of var in narrowing, just use let. Overall using var is a very bad idea nowadays, very bug prone, for example no conflict of variables with the same name. FOr loops use an intermediate variable if needed:
function zzz(o: {q:IUnionType[]}){
let idx = 1;
if (isMulti(o.q[idx])) {
o.q[idx].ans= [];
}
for(let i=0;i<o.q.length;i++){
const item = o.q[i];
if (isMulti(item)) {
item.ans= []; // this case TS know that o.q[0] is IMulti
}
}
for(const item of o.q){
if (isMulti(item)) {
item.ans= []; // this case TS know that o.q[0] is IMulti
}
}
}
本文标签: Union typesGuardwhy error in typescriptStack Overflow
版权声明:本文标题:Union types, guard, why error in typescript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.betaflare.com/web/1739222646a2150775.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


IMultiandISingleare – jcalz Commented yesterday0is constant. Withvar z, TS doesn't try to track it becausevarscope is weird. You can makezaconstor aletfor it to work. Does that fully address the q? If so I'll write an a. – jcalz Commented yesterdayzis constant. It treatso.q[z]as an indexed access expression with an unknown, potentially changing key and doesn't apply narrowing. This should be fixed with theletkeyword – Oluwafemi Sule Commented 12 hours ago