Answers for "typescript typeof object"

1

node js check type of variable

if (typeof i != "number") {
    console.log('This is not number');
}
Posted by: Guest on April-03-2020
2

typescript valueof object

type ValueOf<T> = T[keyof T];
Posted by: Guest on April-29-2020
7

custom types in typescript

// simple type
type Websites = 'www.google.com' | 'reddit.com';
let mySite: Websites = 'www.google.com' //pass
//or
let mySite: Website = 'www.yahoo.com' //error
// the above line will show error because Website type will only accept 2 strings either 'www.google.com' or 'reddit.com'.
// another example. 
type Details = { id: number, name: string, age: number };
let student: Details = { id: 803, name: 'Max', age: 13 }; // pass
//or 
let student: Details = { id: 803, name: 'Max', age: 13, address: 'Delhi' } // error
// the above line will show error because 'address' property is not assignable for Details type variables.
//or
let student: Details = { id: 803, name: 'Max', age: '13' }; // error
// the above line will show error because string value can't be assign to the age value, only numbers.
Posted by: Guest on May-15-2020
0

check type of object typescript

function isFish(pet: Fish | Bird): pet is Fish {
   return (<Fish>pet).swim !== undefined;
}

// Both calls to 'swim' and 'fly' are now okay.
if (isFish(pet)) {
  pet.swim();
}
else {
  pet.fly();
}
Posted by: Guest on September-15-2021
2

typescript type of object values

const data = {
  value: 123,
  text: 'text'
};
type Data = typeof data["text"]; 		// String
Posted by: Guest on April-29-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language