typescript关于void的问题

翻阅了一下ts的教程是说void声明一个变量的时候只有两个值:undefined、null,
无论是英文教程还是中文教程都是这么写的,但是当我写一个最基本的例子时,编译却保错,代码如下:

let unusable: void=null;  // Type 'null' is not assignable to type 'void'.

值为undefined时就没问题

let unusable: void=undefined;

这是为什么?

阅读 6.5k
2 个回答

void

However, when using the --strictNullChecks flag, null and undefined are only assignable to any and their respective types (the one exception being that undefined is also assignable to void).

是不是开了这个 ....

首先我这里let unusable: void=null;是不报错的,有图为证,不知道你用的是那个版本

clipboard.png

By default null and undefined are subtypes of all other types.
That means you can assign null and >undefined to something like number.