Skip to content

Commit c85a6aa

Browse files
committed
Update primitive
1 parent 7139952 commit c85a6aa

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

basics/primitive-data-types.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ let isDone: boolean = false;
2222
```ts
2323
let createdByNewBoolean: boolean = new Boolean(1);
2424

25-
// index.ts(1,5): error TS2322: Type 'Boolean' is not assignable to type 'boolean'.
26-
// 后面约定,注释中标出了编译报错的代码片段,表示编译未通过
25+
// Type 'Boolean' is not assignable to type 'boolean'.
26+
// 'boolean' is a primitive, but 'Boolean' is a wrapper object. Prefer using 'boolean' when possible.
2727
```
2828

2929
事实上 `new Boolean()` 返回的是一个 `Boolean` 对象:
@@ -119,8 +119,6 @@ let u: undefined = undefined;
119119
let n: null = null;
120120
```
121121

122-
`undefined` 类型的变量只能被赋值为 `undefined``null` 类型的变量只能被赋值为 `null`
123-
124122
`void` 的区别是,`undefined``null` 是所有类型的子类型。也就是说 `undefined` 类型的变量,可以赋值给 `number` 类型的变量:
125123

126124
```ts
@@ -140,7 +138,7 @@ let num: number = u;
140138
let u: void;
141139
let num: number = u;
142140

143-
// index.ts(2,5): error TS2322: Type 'void' is not assignable to type 'number'.
141+
// Type 'void' is not assignable to type 'number'.
144142
```
145143

146144
## 参考

examples/test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
function sum() {
2-
var args = arguments;
3-
}
1+
var a = null;
2+
var b = undefined;

examples/test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
function sum() {
2-
let args: number[] = arguments;
3-
}
1+
let a: undefined = null;
2+
let b: null = undefined;

tsconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
// "strict": true
4+
},
5+
"include": ["examples/test.ts"]
6+
}

0 commit comments

Comments
 (0)