美文网首页
any消除技巧

any消除技巧

作者: sweetBoy_9126 | 来源:发表于2020-02-11 16:08 被阅读0次
  1. 数组拍平函数
function flat(arr: Array<any>): Array<any> {
  const result = []
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] instanceof Array) {
      result.push(...arr[i])
    } else {
      result.push(arr[i])
    }
  }
  return result
}
function flat<T>(arr:  Array<T | T[]>) {
  const result: T[] = []
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] instanceof Array) {
      result.push(...arr[i] as T[])
    } else {
      result.push(arr[i] as T)
    }
  }
  return result
}
  1. 类型守卫
Promise.all(newPromise).then((results: Array<[string, string]>) => {
    // results = [['username', undefined], ['password', 'too long']]
    callback(zip(results.filter<[string, string]>(item => !!item[1])))
  })

上面的代码我们filter里面接受的类型肯定是我们最后的返回值类型,而我们最后是要返回[string, string]的,但是她又说我们必须得是一个predicate,所以我们需要单独写一个类型守卫

// item有可能是[string, undefined]也有可能是[string, string],返回的这个item的类型是[string, string],然后返回item[1]的类型是string
function isError(item: [string, undefined] | [string, string]): item is [string, string] {
    return typeof item[1] === 'string'
  }
  Promise.all(newPromise).then((results: Array<[string, string]>) => {
    // results = [['username', undefined], ['password', 'too long']]
    callback(zip(results.filter<[string, string]>(isError)))
  })

相关文章

  • any消除技巧

    数组拍平函数 类型守卫 上面的代码我们filter里面接受的类型肯定是我们最后的返回值类型,而我们最后是要返回[s...

  • 戴愫老师教你与陌生人交谈技巧

    与陌生人交谈技巧分为三个版块, 第一个版块叫做消除紧张,聊出价值。 戴愫老师会教给你几个非常实用的技巧,帮你消除开...

  • macOS 使用的经验技巧

    tags: #macOS #工具笔记by: 不迟any macOS 经验技巧——应用程序相关 强制退出应用程序按下...

  • 2020-03-21

    [太阳] 阅读经典道德经(406) 修道最重要的,就是消除分别心,消除“智慧”,离智慧,离技巧,离心机。世上所有矛...

  • 个人软实力提升特训营

    一、训练主题: 心态上:消除消极情绪,调节好心情; 技能上:学习人与人之间的对话沟通技巧和表达技巧; 二、安排: ...

  • any

    有时候我会花很长的时间去想个名字,微信的、微博的、QQ的。。。,想好了,就改了,然后过了一段时间我会发现我又不喜欢...

  • any

    i can put you. loving we are do the mosten we should meet...

  • 回话是门技术

    陌生搭讪需要无害的方式和技巧,特别需要回话的技术,让人消除戒备心,消除对抗情绪,建立好感,回到平等沟通的频道上来。...

  • 2020-03-18python版本类型注释

    def assertEquals(self, first: Any, second: Any, msg: Any ...

  • python any()和all()用法

    any()用法: any(...) any(iterable) -> bool Return True if bo...

网友评论

      本文标题:any消除技巧

      本文链接:https://www.haomeiwen.com/subject/oxyxfhtx.html