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 :: typescript class example 
Typescript :: namespaces typescript 
Typescript :: what is typescript in angular 
Typescript :: typescript export interface array 
Typescript :: excel check if value exists in range 
Typescript :: props tsx 
Typescript :: how to list elements of an array C# 
Typescript :: nullish coalescing typescript 
Typescript :: generator typescript 
Typescript :: babel typescript 
Typescript :: laravel websockets pusher 
Typescript :: java login attempts using for loop 
Typescript :: typescript ingerit 
Typescript :: undetermined number of arguments in function r 
Typescript :: multicolor points in one legend entry python 
Typescript :: extract digits with serten lenth from string python 
Typescript :: how to get ppt screen shots from a video using python 
Typescript :: localhost magento 2 installation redirects to the live server 
Typescript :: palindromic no. 
Typescript :: piechart am4charts legend with actual values 
Typescript :: install typeorm ts 
Typescript :: require illuminate/console ^8.42|^9.0 - found illuminate/console[v8.42.0, ..., 8.x-dev, v9.0.0-beta.1, ..., 9.x-dev] but these were not loaded, likely because it conflicts with another require. 
Typescript :: typescript transform paths alias 
Typescript :: how do you check ewhich version of typescript you are using 
Typescript :: nest js get request response by index 
Typescript :: - laravel/ui[v3.2.0, ..., 3.x-dev] require illuminate/console ^8.0 - found illuminate/console[v8.0.0, ..., 8.x-dev] but these were not loaded, likely because it conflicts with another require. 
Typescript :: how to find geopoints radius in mongoose 
Typescript :: sprockets cannot load such file sass 
Typescript :: why table columns are messing in one another in angular 
Typescript :: how to keep the state of my widgets after scrolling? flutter 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =