never The never Type

Last updated: 2026-10-22

never The never Type

never means "no value ever": functions that throw or loop forever return it. At the type level it is a subtype of everything, most famous for exhaustive switch checks.

GroupBasic Types
KindType
Strict modeNot required

ℹ️ Info The editor transpiles TypeScript to JavaScript and runs it (no type checking); see each example's Output for type-check results.

📝 Syntax

function fail(msg: string): never {
  throw new Error(msg);
}
// Exhaustiveness: the default branch turns missed cases into compile errors
type Shape = Circle | Square;

⚙️ Parameters

(): neverOnly throw, infinite loops or calls to other never functions — a normal return is an error

Returns:never is itself the return type: the function never returns normally (throws, exits, or loops forever).

▶ Example

Edit the code below and click Try it to see the transpiled result:

Output:

caught: disk full

(Runtime output after transpilation; writing return 1 inside fail is a compile error — never functions cannot return normally)

💡 Notes

ℹ️ Info never is a subtype of every type, yet no value (not even any) is assignable to it — it is the empty set.
💡 Tip Exhaustiveness idiom: const _exhaustive: never = value in the default branch. Adding a union member without handling it fails the build right here (see exhaustive-checks).
⚠️ Note never and void both "return nothing" but differ fundamentally: void ends normally without a value; never never ends normally.
More cases coming soon.

📚 Related tutorials

❓ FAQ

Q never vs void?
A void: the function completes normally with no value (a logger). never: the function never completes normally (throwing/looping), or no value exists at the type level.
Q What is never actually used for?
A Two main uses: annotating throwing/exiting functions, and exhaustive checks — assigning the never-narrowed union in a default branch so missed cases fail the build. Also used to filter union members in conditional types.
Q Why did my variable's type become never?
A Usually over-narrowing: after eliminating all branches nothing remains, or you touched unreachable code. Inspect what the control-flow analysis left — often a duplicated condition.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏