【问题标题】:how to check type of variable in typescript +angular?如何检查打字稿+角度中的变量类型?
【发布时间】:2018-12-13 21:09:21
【问题描述】:

您能告诉我如何在 typescript + angular 中检查变量的 typeof 吗?

import { Component } from '@angular/core';

interface Abc {
  name : string
}
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular 6';
  a:Abc= {
  name:"sss"
  }

  constructor(){
    console.log(typeof this.a)
   // console.log(this.a instanceof Abc) 
  }
}

它应该给truefalse

https://stackblitz.com/edit/angular-jfargi?file=src/app/app.component.ts

【问题讨论】:

标签: angular typescript


【解决方案1】:

只需使用typeof(variable); 所以在你的情况下:

console.log(typeof(this.a));

【讨论】:

    【解决方案2】:

    接口在运行时被擦除,因此在任何运行时调用中都没有接口的痕迹。您可以使用类而不是接口(类在运行时存在并遵守instanceof

    class Abc {
        private noLiterals: undefined;
        constructor(public name: string) { }
    }
    @Component({
        selector: 'my-app',
        templateUrl: './app.component.html',
        styleUrls: ['./app.component.css']
    })
    export class AppComponent {
        name = 'Angular 6';
        a: Abc = new Abc( "sss")
    
        constructor() {
            console.log(this.a instanceof Abc) // Will be true 
        }
    }
    

    或者您可以进行结构检查以查看 Abc 的属性在运行时是否存在于对象中:

    export class AppComponent {
        name = 'Angular 6';
        a: Abc = { name: "sss" }
    
        constructor() {
            console.log('name' in this.a) // Will be true 
        }
    }
    

    【讨论】:

    • 不测试'name' in this.a,给定this.a遵循一个接口,可以:const myInterface = this.a as MyInterface; console.log(myInterface.name)
    【解决方案3】:

    接口 仅在编译时存在,在编译后被删除,因此代码在运行时毫无意义。如果您尝试这样做,它将始终返回false

    看这里-

    constructor(){
        console.log(typeof(this.a), '---');
        console.log(this.instanceOfA(this.a)); 
      }
    
      instanceOfA(object: any): object is ABc {
        return 'member' in object;
      }
    

    Working Example

    更多详情请参考这里-

    【讨论】:

      【解决方案4】:

      试试 'instanceof' 或 'is':

      a instanceof Abc;
      

      另请参阅: Class type check with typescript

      【讨论】:

        【解决方案5】:

        对于基本类型(字符串、数字等),您可以这样检查:

        if( typeof(your_variable) === 'string' ) { ... }

        【讨论】:

          【解决方案6】:

          试试这个

            console.log(typeof (this.specialProviderSelected));
          

          我们可以检查变量的类型,如字符串、数字、对象等

            if((typeof (this.specialProviderSelected)) === 'string') {
               action here...
              }
             if((typeof (this.specialProviderSelected))  === 'number') {
               action here...
              }
              if((typeof (this.specialProviderSelected))  === 'object') {
               action here...
              }
          

          【讨论】:

          • 只有代码的答案几乎总是可以通过添加一些关于它们的工作方式和原因的解释来改进。
          • 下一次,我会尽量给出答案和解释。谢谢你
          • 现在,您可以只edit您的帖子添加一些解释。
          猜你喜欢
          • 2018-11-03
          • 1970-01-01
          • 2019-07-08
          • 1970-01-01
          • 2019-02-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多