Duolith docs

typed-errors

A recipe page with eight type errors on purpose (a misspelt field, a wrong word, a binding of the wrong type, data read before it loaded, a switch that misses a case), to show what duolith check reports. It does not build; expected.txt lists its exact errors, and its tests assert them in the build and the editor.

Run it

cargo run -p duolith-cli -- dev examples/typed-errors     # live, reloads on save
cargo run -p duolith-cli -- build examples/typed-errors   # static output in examples/typed-errors/dist

The folder on the web: examples/typed-errors.

Features it shows

FeatureWhereScreenshot
A misspelt field in markup (E0341)typed-errors pages/index.duo (recipe.titel)duolith check examples/typed-errors
A word an attribute does not take (E0340)typed-errors pages/index.duo (type="serach")the same
A binding whose state has the wrong type, both directions (E0340)typed-errors pages/index.duo (the slider)the same
Props checked against props<...>() (E0340)typed-errors pages/index.duo (minutes="20", level="expert")the same
Data read before it loaded (E0343)typed-errors pages/index.duo (recipes.data.length)the same
A switch that misses a case (E0346)typed-errors pages/index.duo (label)the same

Source

components/RecipeCard.duo
---
import { props } from "@duolith/client";
import type { Level } from "./recipe.ts";

let { title, minutes, level = "easy" } = props<{ title: string; minutes: number; level?: Level }>();
---

<view class="p-4 gap-1 rounded-md bg-surface">
  <text class="text-title text-ink">{title}</text>
  <text class="text-body text-muted">{minutes} min, {level}</text>
</view>
components/recipe.ts
export type Level = "easy" | "medium" | "hard";

export interface Recipe {
  id: number;
  title: string;
  minutes: number;
  level: Level;
}
duolith.yaml
# The recipes come from another site, which the build's Content-Security-Policy
# must allow (E0551), so the errors left are the type errors this example is for.
security:
  content-security-policy:
    connect-src: [https://example.com]
pages/index.duo
---
import { api, state } from "@duolith/client";
import RecipeCard from "../components/RecipeCard.duo";
import type { Level, Recipe } from "../components/recipe.ts";

const server = api("https://example.com");
const recipes = state.load("recipes", () => server.get<Recipe[]>("/recipes"));
let search = state("");
let servings = state("2");

// E0346: "hard" has no case, so the function can end without a value.
function label(level: Level): string {
  switch (level) {
    case "easy":
      return "Quick";
    case "medium":
      return "Weekend";
  }
}
---

<screen title="Recipes" class="p-6 gap-4 bg-canvas">
  <!-- E0340: input takes no type called "serach". -->
  <input type="serach" placeholder="Search" bind:value={search} label="Search" />
  <!-- E0340: a slider's value is a number, and servings holds a string. -->
  <slider bind:value={servings} min={1} max={8} label="Servings" />
  <!-- E0343: the recipes have not loaded yet when this first shows. -->
  <text class="text-body text-muted">{recipes.data.length} recipes</text>
  <list each={recipes.data ?? []} as="recipe" key={recipe.id}>
    <!-- E0341: a recipe has a title, not a titel. -->
    <RecipeCard title={recipe.titel} minutes={recipe.minutes} level={recipe.level} />
    <text class="text-caption text-muted">{label(recipe.level)}</text>
  </list>
  <!-- E0340: minutes is a number, and "expert" is not a level. -->
  <RecipeCard title="Soup" minutes="20" level="expert" />
</screen>