4

谁能解释一下为什么这条 TypeScript 离谱地没有报错?

 1 year ago
source link: https://www.v2ex.com/t/864816
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  TypeScript

谁能解释一下为什么这条 TypeScript 离谱地没有报错?

  makelove · 4 小时 12 分钟前 · 805 次点击
declare type KeyGetter = (obj: unknown) => string

// 取自 lodash
declare const identity: {
  <T>(value: T): T
  (): undefined
}

declare const identity2: {
  <T>(value: T): T
}

declare const identity3: {
  (): undefined
}

let a: KeyGetter = identity // 为啥没报错?!类型不安全了吧
let b : KeyGetter = identity2 // 正常报错
let c: KeyGetter = identity3 // 正常报错

第一个 identity 类型是什么意思?不是一个重载吗?

PlayGround 链接


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK