为什么在块会话是空的,如何修复它?
打印稿沙箱。
type X = {id: string, session: {smt: string} | null}
const x1 = new Promise<X>(resolve => {
resolve('smth' as unknown as X)
})
x1.then((user)=>{
if (!user.session) throw new Error('e')
user.session.smt // ok
return user
})
.then(user=>{
user.session.smt // why session is nullable again?
})
.catch(e=> e)
TypeScript不知道user对象在此期间没有被修改。例如,其他人可能做了:
x1.then((user)=>{
user.session = null; // that's allowed
})