英文:
Typescript does not throw error when required type gets set with optional field
问题
我有这段代码:
let bank_account = await createBankAccount({
acc_number: 'AT' + group.id,
allow_out_payment: true,
bank_id: bank.data[0],
partner_id: group.odoo_id,
})
我的问题是partner_id的类型是数字,而group.odoo_id是可选的。
TypeScript没有标记这个为类型错误。group.odoo_id可能是未定义的,但TypeScript没有任何反应。有什么问题吗?
英文:
I have this code piece:
let bank_account = await createBankAccount({
acc_number: 'AT' + group.id,
allow_out_payment: true,
bank_id: bank.data[0],
partner_id: group.odoo_id,
})
My problem here is that partner_id is of type number and group.odoo_id is optional
Typescript does not mark this as an type error. group.odoo_idcould be undefined but typescript does nothing. Whats wrong here?
答案1
得分: 0
如果你想让TypeScript在这些情况下抛出错误,你需要在tsconfig.json中将选项strictNullChecks设置为true。
然后,如果你确信当前的值已定义,你必须这样标记它:
partner_id: group.odoo_id!,
英文:
If you want TypeScript to throw an error in those cases, you need to set the option strictNullChecks to true in the tsconfig.json.
Then, if you're sure that the current value is defined, you have to mark it like that:
partner_id: group.odoo_id!,
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。




评论