You can use the non-null assertion operator in TypeScript to take a typed variable and remove the undefined
and null
types from it. In this lesson, we look at different ways to work around the null/undefined typed values; including as [type]
and the use of non-null assertion operator which is as simple as adding an exclamation mark to the end of any given variable while reading the value from it.
type User = { name: string, address?: string } const user = { name: "John", address: "abc" } const output: string = user.address // because 'address' is optional, it might be null or undefined. // one way const output: string = user.address as string // better way const output: string = user.address!;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。