zoukankan      html  css  js  c++  java
  • [Typescript] Ignore Null or Undefined Values with TypeScript Non-Null Assertion Operator

    You can use the non-null assertion operator in TypeScript to take a typed variable and remove the undefined and null types from it. In this lesson, we look at different ways to work around the null/undefined typed values; including as [type] and the use of non-null assertion operator which is as simple as adding an exclamation mark to the end of any given variable while reading the value from it.

    type User = {
      name: string,
      address?: string 
    }
    
    const user = {
      name: "John",
      address: "abc"  
    }
    
    const output: string = user.address // because 'address' is optional, it might be null or undefined.
    
    // one way
    
    const output: string = user.address as string
    
    // better way
    
    const output: string = user.address!;
  • 相关阅读:
    *Integer to English Words
    *Palindrome Linked List
    *Partition List
    Sort Colors
    wireshark tls
    find 路径必须在表达式之前
    http://mozilla.debian.net/
    maven bundle
    xmpp
    xmlns=""
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14396157.html
Copyright © 2011-2022 走看看