【问题标题】:TypeScript: function type derive, why type d is true?TypeScript:函数类型派生,为什么类型d为真?
【发布时间】:2020-10-10 08:21:30
【问题描述】:

看看函数 args a 不是扩展类型 never 。但是extends判断是真的,为什么会这样?

type d = ((a: string) => void) extends ((b: never) => void) ? true : false;
let e: d = true;

【问题讨论】:

    标签: typescript extends


    【解决方案1】:

    never 称为bottom type。它是一个空集。空集可以放入任何其他集(例如您的示例中的字符串)。

    正如documentation 所说:

    never 类型是每个类型的子类型,并且可以分配给每个类型;但是,没有任何类型是 never 的子类型或可分配给 never(除了 never 本身)。

    在打字稿中never 用于一个永远不会返回的函数。

    我还建议阅读 TypeScript 中的 differences between void and never

    【讨论】:

    • 是的。永远不适合任何其他集合:`never extends string` 是 true .. 没有类型是 never 所以 string extends never 是 false .. 所以 (a: string) => void) extends ((b: never) => void ) ,函数查询字符串从不扩展???应该是假的
    • 我明白你的意思。无法调用需要 never 参数的函数。因此,任何扩展它的参数都适用于 TS。
    猜你喜欢
    • 2013-10-26
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多