Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

typescript discriminated unions

// Object unions with variants of a similiar property, but different
// key names. Need to add a common property to connect them.
interface Circle {
  kind: "circle";
  radius: number;
}
 
interface Square {
  kind: "square";
  sideLength: number;
}
 
type Shape = Circle | Square;

// TypeScript can now narrow out the members
function getArea (shape: Shape) {
  if (shape.kind === "circle") {
    return Math.PI * shape.radius ** 2 // safely accessing shape.radius
  }
}

// Alternatively use a switch statement
switch(shape.kind) {
  case 'circle':
    return Math.PI * shape.radius ** 2
  case 'square':
    return shape.sideLength ** 2
Comment

typescript unions

type MyBool = true | false;
Comment

PREVIOUS NEXT
Code Example
Typescript :: How to load plugin scripts in roblox studio command 
Typescript :: netsuite suitescript to upload and rename a file 
Typescript :: constraints in database 
Typescript :: how to find matching brackets in eclipse 
Typescript :: cant find the name console 
Typescript :: ts number addition is concatenating like strings 
Typescript :: object map of the http parameters mutually exclusive with fromString 
Typescript :: ex term 15 
Typescript :: running same test in different environment 
Typescript :: accessing python dictionary values with dot 
Typescript :: how to make a tool detect a click and add points roblox studio 
Typescript :: whats the difference between let and const lol 
Typescript :: optional or required depending on param is true react typescript 
Typescript :: let variable name : any = () = { return new typescript fie} 
Typescript :: site:community.nxp.com dts gpio output high active 
Typescript :: get popular posts on laravel 
Typescript :: The create-react-app imports restriction outside of src directory 
Typescript :: sts shortcut to resolve error 
Typescript :: npm run multiple scripts sequentially 
Typescript :: fs readFile binary 
Typescript :: code solutions online for public IActionResult Student() { return View(Students Controller 1); } 
Typescript :: how to print the elements of a array using range based for loop 
Typescript :: how to compile in typescript 
Typescript :: yup validation typescript 
Typescript :: check if that inex exisits array c# 
Typescript :: javascrpit password 
Typescript :: list all motherboard ports command line 
Cpp :: Prime Number Checker 
Cpp :: cpp print vector 
Cpp :: ue4 spawn actor c++ 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =