相关疑难解决方法(0)

打字稿:为什么类型别名满足约束但相同的接口不满足?

我遇到了这个奇怪的案例。我声明了一个条件类型。对于相同的extends约束,类型别名满足它,而结构相同的接口则不满足。

我很失落,为什么不同?检查操场

interface Constraint {
  [key: string]: string | number | boolean
}

type ATypeAlias = {
  str: string
  num: number
  bool: boolean
}

interface SameInterface {
  str: string
  num: number
  bool: boolean
}

type expectToBeTrue = ATypeAlias extends Constraint ? true : false

// Wat???
type butWhyAmIFalse = SameInterface extends Constraint ? true : false
Run Code Online (Sandbox Code Playgroud)

typescript

10
推荐指数
1
解决办法
167
查看次数

标签 统计

typescript ×1