Type checking

2 min·Assumes: components, state

Type checking is TypeScript reading each
as you type, and marking every mistake on your line.
import Avatar from './avatar.tsrx';

export default function Card() @{
	<article>
		<Avatar size="large" />
	</article>
}

Avatar takes a size that is a number, and this file passes the string "large". Your editor draws a red line under size: Type 'string' is not assignable to type 'number'.

Type errors show up as squiggles

A squiggle is the red wavy line your editor draws under a mistake, with TypeScript's message on hover. Click an error in the terminal to jump to it, then click the wrong value to fix it:

A pen-and-ink drawing of an editor with card.tsrx. Line 8 passes size="large" to Avatar, whose size is a number, and has a red squiggle: Type 'string' is not assignable to type 'number'. Line 9 shows titel, a misspelt title, with a squiggle: Cannot find name 'titel'. Did you mean 'title'? Below it, a terminal ran npm run check and printed both errors with their line and column after two pass lines. Clicking an error line jumps the editor to it. Fixing each value clears its squiggle and drops its line from the terminal.

  • Each fix clears its own squiggle.
  • Its line leaves the terminal, until only the pass lines remain.
  • The check runs on every file you open, once your editor is set up.

The check catches real mistakes

These are the messages TypeScript gives in a .tsrx file:

  • A prop of the wrong type. <Avatar size="large" /> gets Type 'string' is not assignable to type 'number'.
  • A misspelt name. {titel}, when the state is called title, gets Cannot find name 'titel'. Did you mean 'title'?
  • A misspelt event handler. <button onClik={...}> gets Property 'onClik' does not exist on type '…'. Did you mean 'onClick'? The type it names is long, so it's cut here.
  • An attribute HTML doesn't have, like className (write class). A camelCase spelling of a real attribute, like strokeWidth for stroke-width, works too: the build writes the real name.
  • A handler whose code doesn't type-check.

npm run check checks the whole app

From the next Markless release, npm run check type-checks every .tsrx file too, so CI can run the same check on each pull request:

$ npm run check
pass: All 8 files are correctly formatted (346ms, 18 threads)
pass: Found no warnings or lint errors in 2 files (299ms, 18 threads)
pages/card.tsrx(8,11): error TS2322: Type 'string' is not assignable to type 'number'.
pages/card.tsrx(9,8): error TS2552: Cannot find name 'titel'. Did you mean 'title'?

Each error gives the file, the line and the column. It exits with code 2 until you fix them, then prints only the two pass lines. npm run build still stops only on Markless errors, so a wrong prop builds.

Editor setup turns the check on.