scrimba
Understanding Typescript In Depth
Typescript Section -4 typeof and instanceof #29
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Typescript Section -4 typeof and instanceof #29
AboutCommentsNotes
Typescript Section -4 typeof and instanceof #29
Expand for more info
index.ts
run
preview
console
const a = "test";
let b: number = 2;
const c: boolean = true;
let d: number | string = "test";
console.log(typeof a); // string
console.log(typeof b); // number
console.log(typeof c); // boolean
console.log(typeof d); // string

let g: number | undefined = undefined;
let h: number | undefined | null = null;
console.log(typeof g);
console.log(typeof h);

// null & undefined
let g: number | undefined = undefined;
let h: number | undefined | null = null;
console.log(typeof g); // undefined
console.log(typeof h); // object
console.log(g === undefined); // true
console.log(g === null); // false

// getting type for union Types

function myFunction(value: number | undefined): void {
console.log("Value is number or undefined");
if (value === undefined) {
console.log("Value is undefined");
} else {
console.log("Value is NOT undefined, hence a number");
}
console.log("Value is number or undefined");
}

// instanceof

class MyClass1 {
memberx: string = "default";
membery: number = 123;
}
class MyClass2 {
memberx1: string = "default";
memberx2: number = 123;
}
const a1 = new MyClass1();
const b1 = new MyClass2();
if (a1 instanceof MyClass1) {
console.log("a === MyClass1");
}
if (b1 instanceof MyClass2) {
console.log("b === MyClass2");
}
Console
"Hello from JavaScript"
,
index.html
-2:37