Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

typescript property does not exist on union type

type Fish = { swim: () => void };
type Bird = { fly: () => void };
 
function move(animal: Fish | Bird) {
  if ("swim" in animal) {
    return animal.swim();
  }
 
  return animal.fly();
}
Try
Comment

union type property does not exist

import { is } from 'typescript-is';

...

const getText = (obj: Obj1 | Obj2): string => {
  if (is<Obj1>(obj)) {
    return obj1.message;
  }

  return obj2.text;
};
Comment

PREVIOUS NEXT
Code Example
Typescript :: typescript read url search params 
Typescript :: t sql if exists multiple conditions 
Typescript :: how to randomly generate a string in ts 
Typescript :: body massage centers in kochi 
Typescript :: Number of power set of {a, b}, where a and b are distinct elements. 
Typescript :: tss from gene granges 
Typescript :: // running tests Your code should no longer have a p tag around the text asking what level ninja a user is. // tests completed category:423 
Cpp :: interpreter latex matlab 
Cpp :: ue4 iterate tmap c++ 
Cpp :: go read file to string 
Cpp :: excel vba delete worksheet if exists 
Cpp :: clear screen in c++ 
Cpp :: tribonacci series c++ 
Cpp :: c++ is string a number 
Cpp :: calculate time difference cpp 
Cpp :: compile cpp with specific version 
Cpp :: map key exists c++ 
Cpp :: compile multiple files C++ linux 
Cpp :: c++ random between two values 
Cpp :: ue log c++ unreal 
Cpp :: resizing dynamic array c++ 
Cpp :: cpp iterate words of string 
Cpp :: creator of C++ 
Cpp :: calculate how many liters would be needed 
Cpp :: priority queue c++ time complexity 
Cpp :: copy substring to another string c++ 
Cpp :: c++ vector add only unique elements 
Cpp :: how to read a line from the console in c++ 
Cpp :: c++ program to calculate discount 
Cpp :: how to make a n*n 2d dynamic array in c++ 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =