T29

Error

Types

T29 — A condition that is not a Bool

if, show, !, && and || take a Bool (or a Bool?, whose null is false). A number, a string or a value that may be null is not one: JavaScript would read 0, "" and null as false, and the program says which it means — count != 0, name != "", user != null. A fallback for a value that may be null is ??, not ||.

What the compiler said

error[T29]: `if` reads `count` as a condition, but it is a number, not a `Bool`: `Number` --> src/App.wf:4:5 | 4 | if count { Text("{count} items") } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = help: Say what it asks: `count != 0` = docs: https://webfluent.monzeromer.dev/errors/T29

Your editor offers this fix: Write != 0. wf fix --code T29 applies it to every place it fits.

The fix

Broken

wf

page P(path: "/", title: "Cart", description: "D") { state count = 0 Heading("Cart").h1 if count { Text("{count} items") } }

Fixed

wf

page P(path: "/", title: "Cart", description: "D") { state count = 0 Heading("Cart").h1 if count != 0 { Text("{count} items") } }

Write the comparison: count != 0, name != "", user != null (which narrows user inside the branch, as if let does). !count is count == 0. A fallback for a value that may be null is ??, not ||: nickname ?? "friend". The editor's quick fix and wf fix --code T29 write each.

Each example is a whole program: it is all of src/App.wf in a project with no other .wf file (trying an example).

Why this rule exists

Error.

if, show, !, && and || take a Bool. JavaScript reads 0, "", null and NaN as false and everything else as true, so if count asks "is it not zero" and if name "is it not empty" without saying so — and if price is false for a price of 0. The program says which it means.

A Bool? is a condition: its null is false. A list, a record or an action is always true, which is T07.

lints cannot lower it: it would ship a broken page.