0
0
Typescriptprogramming~5 mins

Never type and unreachable code in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the never type in TypeScript?
The never type represents values that never occur. It is used for functions that never return or variables that cannot have any value.
Click to reveal answer
beginner
When does TypeScript consider code to be unreachable?
Code is unreachable if it can never be executed, for example, code after a function that always throws an error or an infinite loop.
Click to reveal answer
beginner
What happens if a function has a return type of never?
It means the function never finishes normally. It either throws an error or runs forever, so it never returns a value.
Click to reveal answer
intermediate
Why is the never type useful for exhaustive checks?
It helps catch missing cases in code like switch statements by making sure all possibilities are handled, otherwise TypeScript shows an error.
Click to reveal answer
beginner
Give an example of unreachable code in TypeScript.
Example: <pre>function example() { return; console.log('unreachable'); }</pre> The <code>console.log</code> line is unreachable because the function returns before it.
Click to reveal answer
What does the never type represent in TypeScript?
AA value that never occurs
BA value that can be any type
CA value that is optional
DA value that is null
Which of these is an example of unreachable code?
ACode inside an if statement
BCode inside a function
CCode inside a loop
DCode after a return statement
What will a function with return type never do?
AReturn a string
BNever return normally
CReturn a number
DReturn null
How does TypeScript help with unreachable code?
AIt shows an error or warning for unreachable code
BIt converts unreachable code to comments
CIt deletes unreachable code automatically
DIt ignores unreachable code
Why use never in exhaustive checks?
ATo allow missing cases
BTo ignore errors
CTo catch missing cases at compile time
DTo make code run faster
Explain what the never type means and give an example of a function that returns never.
Think about functions that never finish normally.
You got /3 concepts.
    Describe what unreachable code is and why TypeScript warns about it.
    Consider what happens after a return statement.
    You got /3 concepts.