# Why does a TypeScript default erase an explicit false?

> Test a boolean default with false and an omitted field, then use nullish coalescing to preserve an explicit user choice.

- Canonical HTML: [https://unrust.dev/guides/typescript-explicit-false-challenge](https://unrust.dev/guides/typescript-explicit-false-challenge)
- Markdown representation: [https://unrust.dev/guides/typescript-explicit-false-challenge/index.md](https://unrust.dev/guides/typescript-explicit-false-challenge/index.md)

Published: 2026-10-21
Published by Unrust. Scoring and editorial method: https://unrust.dev/help#methodology

## Direct answer

A type annotation does not change JavaScript evaluation. The || operator chooses the fallback when the left operand is falsy, which includes false. The ?? operator uses its fallback only for null or undefined, so it preserves false. Under this declared input type, omission produces undefined and therefore defaults to true.

## Read the contract before the patch

The optional showHints boolean defaults to true when omitted. An explicitly supplied false must remain false. Inputs are assumed to satisfy the TypeScript type.

Predict the result before reading the repair. Use a small counterexample to separate the code’s behavior from the behavior you expected.

### Original teaching example

```typescript
function showHints(options: { showHints?: boolean }): boolean {
  return options.showHints || true;
}
```



## Compare the repair against the same inputs

A type annotation does not change JavaScript evaluation. The || operator chooses the fallback when the left operand is falsy, which includes false. The ?? operator uses its fallback only for null or undefined, so it preserves false. Under this declared input type, omission produces undefined and therefore defaults to true.

### Repair under the stated contract

```typescript
function showHints(options: { showHints?: boolean }): boolean {
  return options.showHints ?? true;
}
```

**Execution-checked examples, 21 October 2026**

| Input | Original | Repair |
| --- | --- | --- |
| { showHints: false } | true | false |
| { showHints: true } | true | true |
| {} | true | true |



## Extend the review

TypeScript types do not validate untrusted JSON at runtime. Add boundary validation when an input may contain values outside the declared boolean type.

Change one assumption, predict a new outcome, then run the example in your own environment. The public lab repository contains the snippets and their checks. These examples teach review reasoning; they do not measure coding ability.

- [All public lab challenges](https://unrust.dev/guides/public-code-review-challenges/index.md)

- [Runnable lab source and tests](https://github.com/yomariano/unrust-code-review-lab)
- [Download the teaching pack](https://unrust.dev/resources/code-review-lab.json)

## Make your prediction

What does the original function return for { showHints: false }?

- True. Logical OR replaces every falsy left operand.
- False. The optional boolean type preserves the value at runtime.

**Reasoning:** A type annotation does not change JavaScript evaluation. The || operator chooses the fallback when the left operand is falsy, which includes false. The ?? operator uses its fallback only for null or undefined, so it preserves false. Under this declared input type, omission produces undefined and therefore defaults to true.

## Practice loop

1. State the input contract in your own words.
2. Predict each boundary result before running the code.
3. Compare the original and repair with exactly the same input.
4. Write one additional case and explain what it establishes.

## Common questions

### Is this a scored Unrust assessment?

No. This is a fully public, worked teaching example. Its answers are intentionally available and it produces no diagnostic score.

### Can I reuse the example in a tutorial?

Yes. The public lab source is MIT licensed. Keep the license, state the input contract and link to the example and its verification date.

## Limitations

TypeScript types do not validate untrusted JSON at runtime. Add boundary validation when an input may contain values outside the declared boolean type.

## Provenance

Original public teaching material authored for Unrust and execution-checked on 21 October 2026. It is separate from the diagnostic and Daily puzzle banks.

## Sources

- [Nullish coalescing in TypeScript 3.7](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#nullish-coalescing) — TypeScript documentation. Nullish coalescing falls back for null or undefined instead of all falsy values.

## Related guides

- [Free code review challenges with explanations and runnable examples](https://unrust.dev/guides/public-code-review-challenges/index.md): Practice three small JavaScript, Python and TypeScript code reviews. Predict the behavior, inspect a repair and run the original public teaching examples.
- [How do you read code against its contract?](https://unrust.dev/guides/reading-code-against-a-contract/index.md): Learn how to compare implementation with intent, find mismatched assumptions, and catch bugs that clean-looking code can hide.
- [How do you review AI-generated code?](https://unrust.dev/guides/reviewing-ai-generated-code/index.md): Review a JavaScript bug that mishandles zero, compare the corrected code and boundary tests, and learn how to verify AI-generated changes against a contract.

Next step: [Take the free diagnostic](https://unrust.dev/diagnostic/index.md)
