File tree Expand file tree Collapse file tree 1 file changed +7
-5
lines changed Expand file tree Collapse file tree 1 file changed +7
-5
lines changed Original file line number Diff line number Diff line change @@ -679,7 +679,7 @@ let func:StringOrNumberFunc = fn;
679679
680680### strictNullChecks
681681
682- 不打开` strictNullChecks ` 的情况下,变量可以设为 ` undefined ` 或` null ` ,而不管其类型是什么 。
682+ 不打开` strictNullChecks ` 的情况下,一个变量不管类型是什么,都可以赋值为 ` undefined ` 或` null ` 。
683683
684684``` typescript
685685// 不打开 strictNullChecks 的情况
@@ -689,9 +689,9 @@ x = undefined; // 不报错
689689x = null ; // 不报错
690690```
691691
692- 上面示例中,变量` x ` 的类型是` number ` ,但是赋值为` undefined ` 或` null ` 都不会报错。这是为了继承 JavaScript 的设定:当变量没有赋值时,它的值就为` undefined ` 。
692+ 上面示例中,不打开 ` strictNullChecks ` 时, 变量` x ` 的类型是` number ` ,但是赋值为` undefined ` 或` null ` 都不会报错。这是为了继承 JavaScript 的设定:当变量没有赋值时,它的值就为` undefined ` 。
693693
694- 一旦打开` strictNullChecks ` ,就相当于从变量的值里面,排除了 ` undefined ` 和` null ` ,除非变量的类型是这两种类型 。
694+ 一旦打开` strictNullChecks ` ,就使用严格类型,禁止变量赋值为 ` undefined ` 和` null ` ,除非变量原本就是这两种类型。它相当于从变量的值里面,排除了 ` undefined ` 和 ` null ` 。
695695
696696``` typescript
697697// 打开 strictNullChecks 的情况
@@ -701,6 +701,8 @@ x = undefined; // 报错
701701x = null ; // 报错
702702```
703703
704+ 上面示例中,打开` strictNullChecks ` 时,变量` x ` 作为` number ` 类型,就不能赋值为` undefined ` 和` null ` 。
705+
704706下面是一个例子。
705707
706708``` typescript
@@ -709,9 +711,9 @@ x = null; // 报错
709711type A = unknown extends {} ? string : number ;
710712```
711713
712- 上面示例中,` {} ` 代表了 Object 类型,不打开` strictNullChecks ` 时,它包括了` undefined ` 和` null ` ,就相当于包括了所有类型的值,所以这时` unknown ` 类型可以赋值给` {} ` 类型,类型` A ` 就为` number ` 。打开` strictNullChecks ` 时,` {} ` 就排除掉了` undefined ` 和` null ` ,这时` unknown ` 类型就不能赋值给` {} ` 类型后,类型` A ` 就为` string ` 。
714+ 上面示例中,` {} ` 代表了 Object 类型,不打开` strictNullChecks ` 时,它包括了` undefined ` 和` null ` ,就相当于包括了所有类型的值,所以这时` unknown ` 类型可以赋值给` {} ` 类型,类型` A ` 就为` string ` 。打开` strictNullChecks ` 时,` {} ` 就排除掉了` undefined ` 和` null ` ,这时` unknown ` 类型就不能赋值给` {} ` 类型后,类型` A ` 就为` number ` 。
713715
714- 另外 ,` strict ` 属性包含了` strictNullChecks ` ,如果打开` strict ` 属性,就相当于打开了` strictNullChecks ` 。
716+ 最后 ,` strict ` 属性包含了` strictNullChecks ` ,如果打开` strict ` 属性,就相当于打开了` strictNullChecks ` 。
715717
716718### strictPropertyInitialization
717719
You can’t perform that action at this time.
0 commit comments