GVKun编程网logo

TypeScript (二)---- 关于 null、Undefined、never 类型(关于null的说法错误的是)

4

在本文中,我们将带你了解TypeScript(二)----关于null、Undefined、never类型在这篇文章中,我们将为您详细介绍TypeScript(二)----关于null、Undefin

在本文中,我们将带你了解TypeScript (二)---- 关于 null、Undefined、never 类型在这篇文章中,我们将为您详细介绍TypeScript (二)---- 关于 null、Undefined、never 类型的方方面面,并解答关于null的说法错误的是常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的13、TypeScript 之联合类型 - unionType、null、undefined、angular – typescript函数返回undefined、ECMAScript中原始类型Null和Undefined小结、Ionic/Angular TypeScript 错误:Object prototype may only be an Object or null: undefined的解决办法

本文目录一览:

TypeScript (二)---- 关于 null、Undefined、never 类型(关于null的说法错误的是)

TypeScript (二)---- 关于 null、Undefined、never 类型(关于null的说法错误的是)

1 null 和 undefined 

null 是一个只有一个值的特殊类型。表示一个空对象引用。用 typeof 检测 null 返回是 object。

typeof 一个没有值的变量会返回 undefined

ull 和 Undefined 是其他任何类型(包括 void)的子类型,可以赋值给其它类型,如数字类型,此时,赋值后的类型会变成 null 或 undefined。

在 TypeScript 中启用严格的空校验(--strictNullChecks)特性,使得 null 和 undefined 只能被赋值给 void 或本身对应的类型

错误示范:

// 启用 --strictNullChecks
let x: number;
x = 1; // 运行正确
x = undefined;    // 运行错误
x = null;    // 运行错误

 

正确示范:

// 启用 --strictNullChecks
let x: number | null | undefined; //本身对应的类型
x = 1; // 运行正确
x = undefined;    // 运行正确
x = null;    // 运行正确

 

2 never 类型

never 是其它类型(包括 null 和 undefined)的子类型,代表从不会出现的值。这意味着声明为 never 类型的变量只能被 never 类型所赋值,在函数中它通常表现为抛出异常或无法执行到终止点(例如无限循环)。

复制代码
let x: never;
let y: number;

// 运行错误,数字类型不能转为 never 类型
x = 123;

// 运行正确,never 类型可以赋值给 never类型
x = (()=>{ throw new Error(''exception'')})();

// 运行正确,never 类型可以赋值给 数字类型
y = (()=>{ throw new Error(''exception'')})();

// 返回值为 never 的函数可以是抛出异常的情况
function error(message: string): never {
    throw new Error(message);
}

// 返回值为 never 的函数可以是无法被执行到的终止点的情况
function loop(): never {
    while (true) {}
}
复制代码

 

13、TypeScript 之联合类型 - unionType、null、undefined

13、TypeScript 之联合类型 - unionType、null、undefined

所谓联合类型就是指:可以指定多种数据类型,扩大数据类型范围。

let data: string | number | boolean...

TS并不推荐使用

null 和 undefined 是所有类型的子集 有些情况 即使你定义参数类型 传入 null 或者 undefined 也不会报错的

当你 去编译 TS 文件 加上后缀 --strictNullChecks

比如这样tsc filesName.ts --strictNullChecks

var func = function (value: number | string) {
 var type = typeof value;
 if (typeof value === "number") {
  return "your number is " + value;
 }
 else if (typeof value === "string") {
  return "your name is " + value;
 }
};
var result = func(null);

clipboard.png

意思为类型“null”的参数不能赋值给类型 “string | number” 的参数

当然想解决此类问题 只需要在参数加上 null 和 undefined 即可

angular – typescript函数返回undefined

angular – typescript函数返回undefined

您好,我在下面的课程中有一个方法:
export class SearchService {
  userUID: string;
  searchItems: any;
  private searchesCollection: AngularFirestoreCollection;

  constructor(
    private db: AngularFirestore,private afAuth: AngularFireAuth,) {
  }

  getSearches() {
    this.afAuth.authState.subscribe(user => {
      this.userUID = user['uid'];
      this.searchesCollection = this.db.collection(`users/${this.userUID}/searches`);
      this.searchItems = this.searchesCollection.valueChanges().subscribe(data => {
        console.log(data); // works
        return data;
      });
    });
    console.log(this.searchItems); // undefined
    return this.searchItems; //undefined
  }

}

我的问题是return语句,它返回undefined.它上面几行的console.log(data)返回我想要的值.我想知道为什么我会变得不确定.这可能是一个范围问题,但我似乎无法弄明白.它可能是一个我忽略的简单错误.有什么建议么?

谢谢!

您正在使用 async programming ,您无法暂停代码的执行,您的订阅将在未来解决,但您无法预测何时.订阅之外的console.log()在您的订阅被解决之前执行,这就是为什么它是未定义的,并且在订阅被解析之后调用subscribe回调中的console.log().为了更好地理解,请调用 this.
你可以做的是你可以将值存储在类属性中并在模板中访问它.
getSearches() {
    this.afAuth.authState.subscribe(user => {
      this.userUID = user['uid'];
      this.searchesCollection = this.db.collection(`users/${this.userUID}/searches`);
   this.searchesCollection.valueChanges().subscribe(data => {
        console.log(data); // works
         this.searchItems=data;
      });
    });
    console.log(this.searchItems); // undefined
    return this.searchItems; //undefined
  }

HTML

{{searchItems?.//property}}

或者您可以使用异步管道
AsyncPipe接受一个observable或promise作为参数,调用subscribe或附加一个then处理程序,然后在将它传递给调用者之前等待异步结果.

getSearches() {
        this.afAuth.authState.subscribe(user => {
          this.userUID = user['uid'];
          this.searchesCollection = this.db.collection(`users/${this.userUID}/searches`);
       this.searchItems=this.searchesCollection.valueChanges();

      }

HTML

<ng-container *ngFor="let item of searchItems|async">
      {{item?.//property}}
<ng-container>

LIVE DEMO

ECMAScript中原始类型Null和Undefined小结

ECMAScript中原始类型Null和Undefined小结

文章最新版地址:http://leeyee.github.io/blog/2013/05/07/javascript-null-undefined


  var param; var param = undefined; var param = null; 未定义
param == ''undefined'' false false  false 报错。param未定义
param === ''undefined'' false   false   false   报错。param未定义
param == undefined true true true 报错。param未定义
param === undefined true  true  false 报错。param未定义
typeof param undefined   undefined  object undefined 
typeof param == ''undefined'' true  true  false  true 
typeof param === ''undefined'' true  true false  true 
typeof param == undefined false  false  false  false 
typeof param === undefined false false  false  false
if(param) false  false  false  报错。param未定义 
if(typeof param) true true true  true  


结论:

  • null 表示无值,而 undefined 表示一个未声明的变量,或已声明但没有赋值的变量,或一个并不存在的对象属性。
  • 声明但不赋值,默认为undefined;即 var param ; 等价于 var param = undefined;
  • undefined不同于未定义,但typeof并不区分undefined和未定义;因此typeof(undefined)与typeof(未定义变量)的输出值都是undefined;
  • 当函数无明确返回值时,默认返回的为undefined;如:( function foo(){ })
  • typeof(null)返回的是对象,但null == undefined返回true;这是由于值undefined其实是从值null派生过来的,因此ECMAScript将其定义为一样的;
  • null 虽然 == undefined,但undefined是声明了变量但未对其初始化时赋值时赋予该变量的值,null则表示尚未存在的对象;
  • 只能用 === 运算来测试某个值是否是未定义的,因为 == 运算符认为 undefined 值等价于 null;
  • 函数或方法返回值为对象时,如果该返回的对象找不到则该函数或方法通常返回的是null;
  • typeof方法返回的是变量的引用类型的字符名称。因此if(typeof param)为永真。如果要判断引用类型,可以使用instanceof,但此时变量的声明应使用new关键字创建。
测试代码:


function test1() {
          var param;// 定义但未赋值
          console.log("1. var param; --> ", "定义但未赋值");
          console.log(" 1.1. param == ''undefined'' <--> ", param == ''undefined''); // false
          console.log(" 1.2. param == undefined <--> ", param == undefined);// true
         
          console.log("      1.1.1 param === ''undefined'' <--> ", param === ''undefined''); // false
          console.log("      1.2.1 param === undefined <--> ", param === undefined);// true
         
          console.log(" 1.3. typeof param <--> ", typeof param); // undefined
          console.log(" 1.4. typeof param == ''undefined'' <--> ",
                   typeof param == ''undefined''); //true
          console.log(" 1.5. typeof param == undefined <--> ",
                   typeof param == undefined);//false
          console.log("      1.4.1 typeof param === ''undefined'' <--> ",
                   typeof param === ''undefined''); //true
          console.log("      1.5.1 typeof param === undefined <--> ",
                   typeof param === undefined);//false
          param ? console.log(" 1.6. if(param) return true") : console
                   .log(" 1.6. if(param) return false");

          typeof param ? console.log(" 1.7. if(typeof param) return true")
                   : console.log(" 1.7. if(typeof param) return false");

     }
     function test2() {
          console.log("2. param未定义");
          console.log(" 2.1. param == ''undefined'' <--> ", "报错,param未定义");
          console.log(" 2.2. param == undefined <--> ", "报错,param未定义");
          console.log(" 2.3. typeof param <--> ", typeof param); // undefined
          console.log(" 2.4. typeof param == ''undefined'' <--> ",
                   typeof param == "undefined");//true
          console.log(" 2.5. typeof param == undefined <--> ",
                   typeof param == undefined);//false
                         console.log("      2.4.1 typeof param === ''undefined'' <--> ",
                   typeof param === ''undefined'');//true
          console.log("      2.5.1 typeof param === undefined <--> ",
                   typeof param === undefined);//false

          console.log(" 2.6. if(param) 由于param未定义,因此报错");
          typeof param ? console.log(" 2.7. if(typeof param) return true")
                   : console.log(" 2.8. if(typeof param) return false");

     }
     function test3() {
          var param = null;
          console.log("3. var param = null;   -->", "定义默认为null");
          console.log(" 3.1. param == ''undefined'' <--> ", param == ''undefined''); // false
          console.log(" 3.2. param == undefined <--> ", param == undefined);// true
         
          console.log("      3.1.1 param === ''undefined'' <--> ", param === ''undefined''); // false
          console.log("      3.2.1 param === undefined <--> ", param === undefined);// true
         
          console.log(" 3.3. typeof param <--> ", typeof param); // object
          console.log(" 3.4. typeof param == ''undefined'' <--> ",
                   typeof param == ''undefined''); //false
          console.log(" 3.5. typeof param == undefined <--> ",
                   typeof param == undefined);//false
         
           console.log("      3.4.1 typeof param === ''undefined'' <--> ",
                   typeof param === ''undefined''); //false
          console.log("      3.5.1 typeof param === undefined <--> ",
                   typeof param === undefined);//false
         
          param ? console.log(" 3.6. if(param) return true") : console
                   .log(" 3.6. if(param) return false");

          typeof param ? console.log(" 3.7. if(typeof param) return true")
                   : console.log(" 3.7. if(typeof param) return false");

     }
     function test4() {
          var param = undefined;
          console.log("4. var param = undefined;   -->", "定义默认为undefined");
          console.log(" 4.1. param == ''undefined'' <--> ", param == ''undefined''); // false
          console.log(" 4.2. param == undefined <--> ", param == undefined);// true
          console.log(" 4.3. typeof param <--> ", typeof param); // undefined
          console.log(" 4.4. typeof param == ''undefined'' <--> ",
                   typeof param == ''undefined''); //true
          console.log(" 4.5. typeof param == undefined <--> ",
                   typeof param == undefined);//false
          param ? console.log(" 4.6. if(param) return true") : console
                   .log(" 4.6. if(param) return false");

          typeof param ? console.log(" 4.7. if(typeof param) return true")
                   : console.log(" 4.7. if(typeof param) return false");
     }
     test1();
     test2();
     test3();
     test4();


测试结果:


1. var param; --> 定义但未赋值
  1.1. param == ''undefined'' <--> false
  1.2. param == undefined <--> true
       1.1.1 param === ''undefined'' <--> false
       1.2.1 param === undefined <--> true
  1.3. typeof param <--> undefined
  1.4. typeof param == ''undefined'' <--> true
  1.5. typeof param == undefined <--> false
       1.4.1 typeof param === ''undefined'' <--> true
       1.5.1 typeof param === undefined <--> false
  1.6. if(param) return false
  1.7. if(typeof param) return true
2. param未定义
  2.1. param == ''undefined'' <--> 报错,param未定义
  2.2. param == undefined <--> 报错,param未定义
  2.3. typeof param <--> undefined
  2.4. typeof param == ''undefined'' <--> true
  2.5. typeof param == undefined <--> false
       2.4.1 typeof param === ''undefined'' <--> true
       2.5.1 typeof param === undefined <--> false
  2.6. if(param) 由于param未定义,因此报错
  2.7. if(typeof param) return true
3. var param = null;   -->定义默认为null
  3.1. param == ''undefined'' <--> false
  3.2. param == undefined <--> true
       3.1.1 param === ''undefined'' <--> false
       3.2.1 param === undefined <--> false
  3.3. typeof param <--> object
  3.4. typeof param == ''undefined'' <--> false
  3.5. typeof param == undefined <--> false
       3.4.1 typeof param === ''undefined'' <--> false
       3.5.1 typeof param === undefined <--> false
  3.6. if(param) return false
  3.7. if(typeof param) return true
4. var param = undefined;   -->定义默认为undefined
  4.1. param == ''undefined'' <--> false
  4.2. param == undefined <--> true
  4.3. typeof param <--> undefined
  4.4. typeof param == ''undefined'' <--> true
  4.5. typeof param == undefined <--> false
  4.6. if(param) return false
  4.7. if(typeof param) return true

Ionic/Angular TypeScript 错误:Object prototype may only be an Object or null: undefined的解决办法

Ionic/Angular TypeScript 错误:Object prototype may only be an Object or null: undefined的解决办法

出现Object prototype may only be an Object or null: undefined这个问题,有可能是你在基类中导入了子类,所以在编译时就出错了,根据国外网友的回答这个问题可能在TypeScript后续版本中修复

今天关于TypeScript (二)---- 关于 null、Undefined、never 类型关于null的说法错误的是的介绍到此结束,谢谢您的阅读,有关13、TypeScript 之联合类型 - unionType、null、undefined、angular – typescript函数返回undefined、ECMAScript中原始类型Null和Undefined小结、Ionic/Angular TypeScript 错误:Object prototype may only be an Object or null: undefined的解决办法等更多相关知识的信息可以在本站进行查询。

本文标签: