TypeScript 中的可选参数可以为 null 吗? [英] Can an optional parameter be null in TypeScript?

查看:430
本文介绍了TypeScript 中的可选参数可以为 null 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这篇文章,当在打字稿,你不能将 nullundefined 分配给一个变量,除非它被联合明确允许.

According to this article, when strict null checking is enabled in TypeScript, you cannot assign null or undefined to a variable unless it is explicitly allowed with a union.

// required value
let req: string;
req = "Something";  // OK
req = null;      // Error
req = undefined; // Error

// nullable value
let nbl: string | null;
nbl = "Something";  // OK
nbl = null;      // OK
nbl = undefined; // Error

但是在 TypeScript 的 optional 值中允许 null 吗?

But is null allowed in an optional value in TypeScript?

// optional value
let opt?: string; // (actually invalid, as optional types cannot be used for variable declarations, but that's not the point, so imagine we are dealing with function parameters or something)
opt = "Something"; // OK
opt = null; // OK? Error?
opt = undefined; // OK

或者是

opt?: string;

相当于

opt: string | undefined;

因此不允许 null 就像 微软的编码指南?

thus not allowing null just like recommended by Microsoft's Coding guidelines?

推荐答案

Important Note 正如 Quentin C 在下面的评论中指出的那样,此处列出的行为仅在启用了严格空检查时: "strictNullChecks": true in tsconfig.json.

Important Note As Quentin C pointed out in the below comment, the behavior listed here is only when strict null checking is enabled: "strictNullChecks": true in tsconfig.json.

类型 nullundefined 作为单独的类型处理.optional 类型是特殊的,还允许在函数调用中省略参数.

The types null and undefined are handled as separate types. The optional type is special, also allowing arguments to be left out of function calls.

1.如果没有联合或可选,除了类型本身之外,不允许任何其他内容.

function foo(bar: string) {
    console.info(bar);
}

foo("Hello World!"); // OK
foo(null); // Error
foo(undefined); // Error
foo() // Error

2.要额外允许 null,可以与 null 进行联合.

2. To additionally allow null, a union with null can be made.

function foo(bar: string | null) {
    console.info(bar);
}

foo("Hello World!"); // OK
foo(null); // OK
foo(undefined); // Error
foo() // Error

3.允许 undefined 的工作原理类似.请注意,该参数不能被遗漏或 null.

3. Allowing undefined works similarly. Note that the argument cannot be left out or null.

function foo(bar: string | undefined) {
    console.info(bar);
}

foo("Hello World!"); // OK
foo(null); // Error
foo(undefined); // OK
foo() // Error

4.您也可以同时允许两者,但仍必须给出参数.

function foo(bar: string | null | undefined) {
    console.info(bar);
}

foo("Hello World!"); // OK
foo(null); // OK
foo(undefined); // OK
foo() // Error

5.使用 optional 你可以省略参数,或者传递 undefined,但不是 null.

5. With optional you can leave the argument out, or pass undefined, BUT NOT null.

function foo(bar?: string) {
    console.info(bar);
}

foo("Hello World!"); // OK
foo(null); // Error
foo(undefined); // OK
foo() // OK

6.为了允许所有三种特殊情况,可以组合 optionalnull.

6. To allow all three special cases, optional and null can be combined.

function foo(bar?: string | null) {
    console.info(bar);
}

foo("Hello World!"); // OK
foo(null); // OK
foo(undefined); // OK
foo() // OK


此外,optional 仅可用于参数或其他类型声明,例如接口,不能用于常规变量.因为在赋值时省略变量的值是没有意义的.


Also, optional is only usable in parameter or other type declarations, such as interfaces, not on regular variables. As it would make no sense to leave out the value of a variable when assigning to it.

因此,

let d?: string;

没有意义并导致编译错误.

would make no sense and results in a compilation error.

这篇关于TypeScript 中的可选参数可以为 null 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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