0
点赞
收藏
分享

微信扫一扫

TypeScirpt 特别的运算符号

眼君 2022-04-24 阅读 32
jstypescript
  • ?:表示属性或参数为可选项
    interface Person {
    	name: string,
    	age: number,
    	weight?: number
    }
    
    const person = new Person(name='Bob', age=18)
    
  • ??:空值合并运算符。当左侧操作数为 null 或 undefined 时,其返回右侧的操作数,否则返回左侧的操作数。
    console(person.weight ?? 0)
    
  • !:表示类型推断排除 nullundefined
    function myFunc(maybeString: string | undefined | null) {
      const onlyString: string = maybeString; // Error
      const ignoreUndefinedAndNull: string = maybeString!; // Ok
    }
    
  • !!:用于将类型转换为布尔值。其中 nullundefined0'' 转换后为 false,其他都为 true
    let a = null
    console.log(!!a) // false
    
    let b = 'hello world'
    console.log(!!b) // true
    
    let bob = {name: 'Bob', age: 18}
    console.log(!!bob) // true
    
举报

相关推荐

0 条评论