Typescript - 可选类型

2024-03-14

如何在 Typescript 中使用可选类型? 例如我有一个名为Conversation:

class Conversation {
    lastMessage?: Message
}

class Message {
    text: string
}

我想要可以为空text from Message对象通过Conversation object.

const lastMessageText?: string = conversation?.lastMessage.text

但这个语法不起作用。我写不了?谈话结束后。


从...开始TypeScript 3.7 optional chaining可用。

发行说明3.7 here https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#optional-chaining.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Typescript - 可选类型 的相关文章

随机推荐