What is the difference between never and void in typescript?

前端 未结 8 2367
刺人心
刺人心 2020-12-15 15:04

I have read this, but it is unclear what would be the difference between \'never\' and \'void\' type?

相关标签:
8条回答
  • 2020-12-15 15:47

    As Marius Schulz discusses in this article,

    • A function that doesn't explicitly return a value implicitly returns the value undefined in JavaScript. Although we typically say that such a function "doesn't return anything", it returns. We usually ignore the return value in these cases. Such a function is inferred to have a void return type in TypeScript.
    • A function that has a never return type never returns. It doesn't return undefined, either. The function doesn't have a normal completion, which means it throws an error or never finishes running at all.
    0 讨论(0)
  • 2020-12-15 15:48

    The return type of Promise.reject() is Promise<never>, meaning "it is never resolved".

    So if a function returns Promise<never>, I think it will return only errors. On the other hand, Promise<void> might be resolved without value.

    0 讨论(0)
提交回复
热议问题