void
undefined:变量没有赋值,没有初始化
void:变量本身就不存在
never:一个函数永远执行不完
一个函数永远执行不完,never
函数返回类型
void:不返回值
function printResult():void {
....
}
undefined:变量没有赋值,没有初始化
function printResult():void {
....
return ;
}
never:一个函数永远执行不完,不常用
function throwError(message:string ,errorCode:number):never{
throw{
message,
errorCode
}
}
function whileLoop():never{
while(true){}
}
void unefined never
函数不返回的时候就是void类型的函数
函数返回的值的类型undefined
never
函数抛出异常,永远无法执行完成
throw 抛出异常 或者 while循环 可以用来偷懒
// void、undefined 与 never
function printResult () : void {
console.log("lalala");
}
console.log(printResult()); // 报错
function printResult () : undefined {
console.log("lalala");
}
console.log(printResult());
function throwError(message: string, errorCode: number) :never{
throw {
message,
errorCode
}
}
throwError("not found", 404);
function whileLoop(): never {
while(true) {
console.log("hahah")
}
}
函数返回值类型 void undefined never
void 不存在
undefined 未定义
never 函数永远无法执行完成,大部分情况下用来处理异常
void没返回值的函数,变量本身就不存在
undefined有返回值的函数,变量没赋值不存在
never跳出并且终止继续执行函数,执行不完的函数,如throw或用while(true){}
viod 函数返回值不存在
undefined 返回值未声明
never 函数未执行完成
function aaa():viod{
}
function aaa():undfined{
return
}
function aaa(message:string):never{
throw{
message
}
}
1、void:
函数在没有任何返回的情况下,函数就是一个 void 类型。
funciton printResult() {
console.log('lalalla');
}
也可以给函数指定类型:
function printResult() : void {
console.log('lalalla');
}
console.log('hello ', printResult()); //hello undefined
在原生JavaScript中没有void对应的表述的。
2、undefined:
function printResult() : undefined {
console.log('lalalla');
return;
}
使用过JavaScript的都知道,其实undefined也是一个值,是一个未经初始化的值。
使用undefined的时候可以说某个东西是undefined,但使用void,表示这个东西不存在。
两者都表示没有,undefined说的是变量没有赋值没有初始化,而void指定是变量本身就不存在。
3、never:(一个函数永远执行不完,这就是 never 的本质。实际开发过程中,never是用来控制逻辑流程的,大部分是用来处理异常或者处理promise)
function throwError(message: string, errorCode: number) : never {
throw { //抛出异常,强行结束
message,
errorCode
}
//永不会执行到这里,这个函数永远不会执行完成,因此这个函数类型是 never
}
throwError('not found', 404);
function whileLoop() : never {
while(true) {
console.log('hhh');
}
}
解决返回返回undefined的问题