【问题标题】:what's the difference between Record<string, any> and Record<string, unkown> in typescript?打字稿中 Record<string, any> 和 Record<string, unknown> 有什么区别?
【发布时间】:2021-03-13 02:51:10
【问题描述】:

我在练习打字,然后遇到这种问题,看了一些帖子还是不明白是什么原因。

type A = (() => true) extends Record<string, any> ? true : false; // => true
type B = (() => true) extends Record<string, unknown> ? true : false; // false

【问题讨论】:

标签: javascript typescript frontend


【解决方案1】:

anyunknown 之间的基本区别在于我们将其分配给其他变量的方式。 unknown 是类型安全的,因此当您将带有 unkown 的变量分配给具有特定类型的其他变量时是不可能的。

我认为您可以阅读有关这两个帖子的更多信息。

  1. 'unknown' vs. 'any'
  2. https://mariusschulz.com/blog/the-unknown-type-in-typescript

【讨论】:

  • 这个答案没有解释为什么箭头函数 Record&lt;string, any&gt;不是 Record&lt;string, unknown&gt;
猜你喜欢
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
  • 2018-07-16
  • 2022-01-20
  • 2023-01-18
  • 2021-03-06
  • 1970-01-01
  • 2021-11-26
相关资源
最近更新 更多