TypeScript文档说
该
never类型是每种类型的子类型,并且可以分配给每种类型
但没有提及原因。
凭直觉,我希望这样的代码会失败:
const useString = (str: string) => console.log('This is definitely a string:', str)
const useNever = (not_a_string: never) => useString(not_a_string)
Run Code Online (Sandbox Code Playgroud)
但没有错误,因为任何never值都被视为有效字符串。
这是故意的吗?如果是,那为什么呢?:)
typescript ×1