问号Typescript变量 [英] Question mark Typescript variable

查看:660
本文介绍了问号Typescript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这样的代码片段:

I've seen code snippets like these:

export interface IUser {
    email?: string;
    firstName?: string;
    lastName?: string;
}

但是为什么变量名带有问号后缀?此代码段是将Mongodb与Typescript结合使用的示例的一部分.

But why are the variable names suffixed by a question mark? This snippet is part of an example of using mongodb with Typescript.

答案可能在某处,但是由于找不到,我似乎使用了错误的关键字.

The answer is probably somewhere out there but I seem to be using the wrong keywords since I can't find it.

推荐答案

在TypeScript中,<name>?: <typename><name>: <typename> | undefined的简写. 它向类型系统指示符号可能包含指示类型的值,或者它可能包含值undefined(类似于null).

In TypeScript, <name>?: <typename> a shorthand for <name>: <typename> | undefined. It indicates to the type system that a symbol may contain a value of the indicated type or it may contain the value undefined (which is like null).

启用(TypeScript 2中的新功能)--strictNullChecks选项时,这一点很重要.关于可识别空和未定义类型的文件选项可能是您应该开始了解为什么这样做有用的地方.

This is important when the (new in TypeScript 2) --strictNullChecks option is enabled. The documentation on Null- and undefined-aware types option is probably where you should start to understand why this is useful.

这篇关于问号Typescript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆