zoukankan      html  css  js  c++  java
  • typescript-函数参数和返回类型的定义

    方法的返回类型是number

    function getTotal(one: number, two: number): number { //返回类型是number
      return one + two
    }
    const total = getTotal(1, 2)
    console.log(total)

    没有任何返回值

    function sayHello(): void { //没有任何的返回值
      console.log('hello')
    }

    函数执行无法完成 抛出错误或者死循环

    function errorfunction(): never {//抛出错误后 无法执行完成
      throw new Error()
      console.log('hello error')
    }
    
    function forNever(): never {//永远无法结束的  
      while (true) {
        console.log('forNever')
      }
    }

    函数参数是对象

    function add({ one, two }: { one: number, two: number }) { //参数是个对象的
      return one + two
    }
    const t = add({ one: 1, two: 2 })
    
    
    function getNumber({one}:{one:number}){
      return one
    }
    const o=getNumber({one:1})
  • 相关阅读:
    数据操作-apply函数族
    11.盛水最多的容器
    canvas绘图
    Nodejs事件监听模块
    http性能测试
    源码解读
    nodejs的一些概念
    http知识补充
    querystring处理参数小利器
    url网址解析的好帮手
  • 原文地址:https://www.cnblogs.com/lxz-blogs/p/13564146.html
Copyright © 2011-2022 走看看