T18

Error

Types

T18 — Arithmetic on something that is not a number

-, *, / and % take numbers (or scale a duration); on a string or a list the result is NaN. + adds two numbers, joins two strings or adds two durations — text and a number are not joined by +, a splice says how the number is shown: "Total: {n}".

What the compiler said

error[T18]: `*` takes numbers, but `price` is `String` --> src/App.wf:3:27 | 3 | Heading("Total {price * 2}").h1 | ^ = help: Convert it: `Number(value)` = docs: https://webfluent.monzeromer.dev/errors/T18

The fix

Broken

wf

page P(path: "/", title: "T", description: "D") { state price = "12" Heading("Total {price * 2}").h1 }

Fixed

wf

page P(path: "/", title: "T", description: "D") { state price = 12 Heading("Total {price * 2}").h1 }

Hold numbers as numbers (state price = 12), or convert: Number(price) * 2.

+ joins two strings or adds two numbers. Text and a number joined with + is refused too, since nothing would say how the number is shown:

page P(path: "/", title: "T", description: "D") {
    state n = 3
    Heading("Cart").h1
    Text("Total: " + n)
}
error[T18]: `+` joins two strings or adds two numbers, but `n` is `Number`
 --> src/App.wf:4:20
  |
4 |     Text("Total: " + n)
  |                    ^
  = help: Splice it into the text, which says how it is shown: `"Total: {n}"`
  = docs: https://webfluent.monzeromer.dev/errors/T18
page P(path: "/", title: "T", description: "D") {
    state n = 3
    Heading("Cart").h1
    Text("Total: {n}")
}

Fix: Splice the value into the text — "Total: {n}", or "Total: {n:.currency}" to say how it shows. The editor's quick fix, wf fix --code T18 and wf migrate write the splice.

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

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