codestory

Typeof-Operator in der TypeScript-Sprache

  1. Der Operator typeof
  2. Der Operator instanceof

1. Der Operator typeof

Der Operator typeof gibt einen string zurück, der den Datentyp eines ausgewerteten Werts darstellt.
Die Syntax:
let result = typeof aValue;
Zum Beispiel:
typeof_ex1.ts
console.log(typeof 42); // "number"

console.log(typeof 'blubber'); // "string"

console.log(typeof true); // "boolean

let aVariable; // A variable is not initialized.
console.log(typeof aVariable); // "undefined"
Grundsätzlich gibt der Operator typeof nur einen der folgenden Werte zurück:
1
undefined
"undefined"
2
null
"object"
3
Boolean
"boolean"
4
number
"number"
5
BigInt
"bigint"
6
string
"string"
7
Symbol
"symbol"
8
Function object
"function"
9
Any other object
"object"
  • TypeScript BigInt
  • TypeScript Symbol
  • TypeScript Boolean
  • Closures
  • Numbers
  • String
  • Objects
Zum Beispiel:
typeof_ex2.ts
console.log(' --- (1) typeof undefined -- ');
console.log(typeof undefined); //
let aValue; // A variable is not initialized
console.log(typeof aValue);  // "undefined"

console.log(' --- (2) typeof null -- ');
console.log(typeof null); // "object"

console.log(' --- (3) typeof Boolean -- ');
console.log(typeof true); // "boolean"
console.log(typeof (3 > 5)); // "boolean"

console.log(' --- (4) typeof Number -- ');
console.log(typeof 100); // "number"

console.log(' --- (5) typeof bigint -- ');
console.log(' ** See the bigint article for how to use the bigint library.');

console.log(' --- (6) typeof string -- ');
console.log(typeof "Tom"); // "string"

console.log(' --- (7) typeof Symbol -- ');
let aSymbol = Symbol(123);
console.log(typeof aSymbol); // "symbol"

console.log(' --- (8) typeof Function (or Closure) -- ');
let aFunction = function() {
    console.log('Hello');
};
console.log(typeof aFunction); // "function"

console.log(' --- (8) typeof Any-Other-Object -- ');
var anObject1 = {name: 'Tom', salary: 1000};
var anObject2 = new Object();
console.log(typeof anObject1); // "object"
console.log(typeof anObject2); // "object"
Output:
--- (1) typeof undefined --
undefined
undefined
--- (2) typeof null --
object
--- (3) typeof Boolean --
boolean
boolean
--- (4) typeof Number --
number
--- (5) typeof bigint --
** See the bigint article for how to use the bigint library.
--- (6) typeof string --
string
--- (7) typeof Symbol --
symbol
--- (8) typeof Function (or Closure) --
function
--- (8) typeof Any-Other-Object --
object
object
Beispiel: Verwenden Sie den Operator typeof, um den an einen Konstruktor übergebenen Datentyp zu bestimmen:
typeof_ex3.ts
class Song {
    title: string;
    duration: string; // "minutes:seconds".
     
    constructor(title: string, duration: string | number) {
        this.title = title;
        if(typeof duration === 'string') {
            this.duration = duration;
        } else {
            let seconds = duration % 60;
            let minutes = Math.floor(duration / 60);
            this.duration = minutes + ":" + seconds;
        }
    }
}
let mySong1 = new Song("Hotel California", "6:30");
console.log(`mySong1.duration = ${mySong1.duration}`); // 6:30

let mySong2 = new Song("My Heart Will Go On", 270); // seconds
console.log(`mySong2.duration = ${mySong2.duration}`); // 4:30
Output:
mySong1.duration = 6:30
mySong2.duration = 4:30

2. Der Operator instanceof