Complete the code to declare a variable with type never.
let errorValue: [1];The never type represents values that never occur. It is used for variables that never hold any value.
Complete the function signature to indicate it never returns.
function fail(message: string): [1] {
throw new Error(message);
}The function throws an error and never returns normally, so its return type is never.
Fix the error in the exhaustive check function by completing the switch default case.
function assertNever(x: never): never {
throw new Error("Unexpected value: " + [1]);
}typeof x which returns a string describing the type, not the value.JSON.stringify(x) which may cause runtime errors if x is never.The parameter x is of type never, so passing it to the error message directly is correct.
Fill both blanks to complete the exhaustive check in the switch statement.
type Shape = { kind: 'circle', radius: number } | { kind: 'square', size: number };
function area(shape: Shape): number {
switch (shape.kind) {
case 'circle':
return Math.PI * shape.radius * shape.radius;
case 'square':
return shape.size * shape.size;
default:
return [1](shape); // call assertNever
}
}
function [2](x: never): never {
throw new Error('Unexpected shape: ' + x);
}The function assertNever is called in the default case to ensure all cases are handled.
Fill all three blanks to complete the exhaustive check with a switch and assertNever function.
type Result = { type: 'success', value: number } | { type: 'error', message: string };
function handleResult(result: Result): string {
switch (result.type) {
case 'success':
return `Value is ${result.value}`;
case 'error':
return `Error: ${result.message}`;
default:
return [1]([2]); // call assertNever with result
}
}
function [3](x: never): never {
throw new Error('Unexpected result: ' + x);
}The default case calls assertNever with the variable result to ensure all cases are handled.