最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

TS 類型收窄教程示例詳解

 更新時(shí)間:2022年09月20日 14:20:34   作者:dingsheng  
這篇文章主要為大家介紹了TS 類型收窄教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

類型收窄之前只能使用公共方法

JS方法

typeof

缺點(diǎn)

  • typeof null —→ object
  • typeof 數(shù)組 —→ object
  • typeof 日期 —→ object

a instanceof A : A 是否出現(xiàn)在a的原型鏈上

缺點(diǎn)

不支持stringnumber 、boolean 等原始類型

不支持TS的 自定義類型,如下:

type Person {
  name: string
}
  • key in obj
  • Array.isArray()

????類型謂詞is

重點(diǎn)在 shape is Rect

type Rect = {
  width: number
  height: number
}
type Circle = {
  center: [number, number]
  radius: number
}
const area = (shape: Rect | Circle): number => {
  if(isRect(shape)) {
    return shape.width * shape.height
  } else {
    return Math.PI * shape.radius ^ 2
  }
}
const isRect = (shape: Rect | Circle): shape is Rect => {
  return 'width' in shape && 'height' in shape
}

????????可辨別聯(lián)合

要求:T = A | B | C

  • A | B | C … 要有一個(gè)相同的屬性 type或其它
  • type類型只能為 簡(jiǎn)單類型
  • A | B | C …的type屬性無(wú)交集
type Rect = {
  type: 'rect',
  width: number
  height: number
}
type Circle = {
  type: 'circle'
  center: [number, number]
  radius: number
}
const area = (shape: Rect | Circle): number => {
  if(shape.type === 'rect') {
    return shape.width * shape.height
  } else {
    return Math.PI * shape.radius ^ 2
  }
}

以上就是TS 類型收窄教程示例詳解的詳細(xì)內(nèi)容,更多關(guān)于TS 類型收窄的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:

相關(guān)文章

最新評(píng)論

沁水县| 仪征市| 大埔县| 奉节县| 阿拉善盟| 曲靖市| 鹿泉市| 扎鲁特旗| 定南县| 天台县| 普兰县| 格尔木市| 沁阳市| 尼勒克县| 霸州市| 灵武市| 化州市| 宽城| 乌海市| 广南县| 阳新县| 德安县| 吉水县| 兖州市| 浦城县| 乾安县| 保山市| 响水县| 泸定县| 白城市| 济源市| 新野县| 伊春市| 合川市| 阿勒泰市| 盐边县| 吉木乃县| 肥东县| 南漳县| 资中县| 塔城市|