TypeScript
elements man typescriptElements is the first build tool to build the TypeScript compiler into itself. The compiler's Go source code is part of the Elements source code and compiles into the same binary, so it is not a separate program or a linked library. Type checking is a phase of the build, running in the same process against the same in-memory source graph as compilation, so a save is checked, compiled, and hot reloaded in milliseconds. Both targets are checked, the browser and the server, each against the types it actually has.
One source graph
Every source in a project has a version derived from its content and the versions of everything it imports. The checker reads that version like every other phase of the build: a file whose version has not changed is not rechecked, and a change to a file rechecks that file and every file that imports it, directly or through other files. The build cache, the test runner, hot reload, and the deploy read the same versions, so type checking, compilation, and release agree on one version of every file.
Checking runs incrementally inside the build. A single-file save type checks, compiles, and hot reloads in milliseconds.
Type checking results are a build query
Type checking runs only when a file changes, and then only on the files the
change affects: that file and every file that imports it. The build server
runs while you work and keeps the result of the last type check in memory. That state is preserved between commands, so asking for the
type errors does not run the checker again. elements build -json is a query
against the build server: it returns the current diagnostics, and the answer
arrives in microseconds. Your terminal, your editor and your agent all ask the
same build server, so each sees the same errors at the same moment.
The Elements languages are built into the TypeScript checker
Elements HTML templates and JSOC config files are their own languages, built
into the TypeScript compiler's binder and checker rather than beside them. A
template attribute is declared like a TypeScript declaration. A template name
resolves through the normal symbol table. The expressions inside {} are
checked against the surrounding scope by the same checker that checks a .ts
file, with the same error messages. There is no second type system to learn:
one checker checks .ts files, templates, and JSOC files.
Example<Comment ( comment: Comment, private editing: boolean = false, )> <p>{comment.text.toUpperCase()}</p> <p>{comment.txet}</p> <!-- error: Property 'txet' does not exist on type 'Comment' --> </Comment>
Server and browser targets
A file is checked once for each target that loads it. Code that runs on the
server is checked against Node's types and the parts of @elements/app that
exist on the server. Code that runs in the browser is checked against the DOM
types and the parts of @elements/app that exist in the browser. A
server-only call such as sql() in code that runs in the browser is a compile
error at the call site, so a query or a secret is never sent to the browser by
accident. You do not annotate anything; the compiler
follows the call graph. See rpc for which code runs on the server and async for how
sync-style calls type check without await.
Options
TypeScript options are set under build.typescript in config.jsoc. Its
keys are the tsconfig compilerOptions that apply to an Elements build.
Strict checking is on. There is no tsconfig.json in an Elements project and
you should not add one.
config.jsocbuild: { typescript: { lib: ["es2022", "dom"], }, },
Full config syntax: config.
Editors
The build server implements the Language Server Protocol, so editors get
completions, diagnostics, go-to-definition, and rename for .ts files, HTML
templates, and JSOC alike, from the same checker the build uses. It replaces
tsserver.
There is nothing to set up. Installing Elements installs the editor
integration: the extension for VS Code and its derivatives, and the plugin for
Vim and Neovim. No plugin manager, no .vimrc or init.lua changes. For what
Vim and Neovim get and how to set it up by hand instead, see editors/vim;
for VS Code, editors/vscode.
Diagnostics
Type errors are build errors. They appear in elements build, in
elements build -json as diagnostics with a path and a 1-based loc, and in
the editor, all from one check. A build with a type error anywhere does not
write a new release, and a test whose dependencies have the error does not
run until the error is fixed. See build.
Related
build: the build loop, which runs the checker.html: the template language the checker understands.config:build.typescript.async: the sync-style transform and how it types.