Answers for "typescript type definition"

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
1

make a type in typescript

type Props = {
  item: CartItemType;
  addToCart: (clickedItem: CartItemType) => void;
  removeFromCart: (id: number) => void;
};
Posted by: Guest on September-06-2021
1

type of typescript

// Prints "string"
console.log(typeof "Hello world");
Posted by: Guest on October-06-2021
1

typing in typescript

var name: string = "Anna";
let notes: (number | string)[] = ["Get Food", 23, "Call the previous number when betting"];
Posted by: Guest on September-21-2020

Code answers related to "typescript type definition"

Browse Popular Code Answers by Language