33

could you please tell me how to check typeof of variable in typescript + angular ?

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) 
  }
}

It should give true and false

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

2

6 Answers 6

38

Interfaces are erased at runtime so there will be no trace of the interface in any runtime call. You can either use a class instead of an interface (classes exist at runtime and obey 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 
    }
}

Or you can do structural checking to see if the properties of Abc are present at runtime in the object:

export class AppComponent {
    name = 'Angular 6';
    a: Abc = { name: "sss" }

    constructor() {
        console.log('name' in this.a) // Will be true 
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Instead of testing 'name' in this.a, given this.a follows an interface, one could: const myInterface = this.a as MyInterface; console.log(myInterface.name)
structural check works, however instanceof for class-es is a bit tricky. They only work if you initialize the variable with the new keyword. If initialized as an object, it won't fly. See: stackblitz.com/edit/…
I create a class which implements the interface. Then I initialize properties in the constructor of that class. This way I can new up an object at runtime and pass it around as the interface type, if need be.
36

just use typeof(variable); so in your case:

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

Comments

9

Try 'instanceof' or 'is':

a instanceof Abc;

See also: Class type check with typescript

Comments

6

For basic types (string, number, etc) you can check like this:

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

Comments

3

Interfaces only exist at compile-time and are removed after compilation, so that code makes no sense at run-time. If you try so it will always return false.

Have a look here -

constructor(){
    console.log(typeof(this.a), '---');
    console.log(this.instanceOfA(this.a)); 
  }

  instanceOfA(object: any): object is ABc {
    return 'member' in object;
  }

Working Example

For more in details refer here -

Comments

1

try this

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

we can check type of variable like as string, number, object etc

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

3 Comments

Code only answers can almost always be improved by the addition of some explanation of how and why they work.
next time , i will try to give answers with explanations. thank u
For now, you can just edit your post to add some explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.