One TypeScript checker for your editor, terminal, and agent

10

In this video we show how the Elements build server type checks TypeScript. The Elements build server runs the TypeScript type checker inside the build server process. When you save a file, the build server runs a build, and that build type checks the file you saved. The build server sends the same type errors to your editor, to your terminal and to `elements build -json`, the command an agent runs. In the video we edit the code of a demo app. We pass a number to a function that takes a string. We add a function that calls `toSorted`, which was added to JavaScript in ES2023, to a file that runs on the server and in the browser. The build server type checks that file twice: once against the library types that the `lib` option lists for the server, and once against the library types that the `lib` option lists for the browser. The demo app's `config.jsoc`, the project's config file, sets `lib` to ES2023 for the server and ES2020 for the browser, so the `toSorted` call is a type error for the browser only. The demo app's home page is a reactive HTML file. Reactive HTML is our HTML language: plain HTML tags, with TypeScript expressions in a tag's attributes and in its body. The home page imports a variable named `greeting` from a TypeScript file and prints the value of `greeting`. We misspell `greeting` in the home page, and the build server reports a type error. Then we explain which files the build server type checks after you save a file. ## One Type Error in the Editor, the Terminal and the `elements build` Command an Agent Runs The demo app in the video has a TypeScript file, `app/shared/greet.ts`, with one function, `greet`, and one call to `greet`: ```ts app/shared/greet.ts export function greet(name: string): string { return "Hello, " + name; } export let greeting = greet("Ada"); ``` `greet` takes a string. On line 5 we pass `greet` a number: ```ts app/shared/greet.ts export let greeting = greet(42); ``` Passing a number to `greet` is a type error, and the editor underlines `42`. We save `greet.ts`, and the Elements build server runs a build. The build server type checks `greet.ts`. `elements start` is running in a terminal. `elements start` connects to the build server and runs the demo app. After each build, `elements start` prints `State: Ok` or `State: Error`, and under `State: Error` the errors from that build. After the build that ran when we saved `greet.ts`, `State: Ok` changes to `State: Error`, and `elements start` prints the type error: ![The output of elements start in a terminal: State: Error, then one error at app/shared/greet.ts:5:29, Argument of type 'number' is not assignable to parameter of type 'string'. The error code is TS2345.](/images/01a2354d-43d4-7131-aaaf-125e8c17cfa0/f4b841d01b248c15?w=1480&h=624#light) ![The output of elements start in a terminal: State: Error, then one error at app/shared/greet.ts:5:29, Argument of type 'number' is not assignable to parameter of type 'string'. The error code is TS2345.](/images/01a2354d-43f0-7d77-b134-9fc6c5120544/89ab92847a3e9824?w=1480&h=624#dark) To read the errors from the last build, an agent runs `elements build -json`. In the video we run `elements build -json` in a second terminal: ```bash Terminal elements build -json ``` The build server returns the same type error, and the `elapsedSeconds` field says the build server answered in one microsecond: ```json "elapsedSeconds": 0.000001, "diagnostics": [ { "code": 2345, "level": "error", "message": "Argument of type 'number' is not assignable to parameter of type 'string'.", "path": "app/shared/greet.ts", "loc": { "start": 104, "finish": 106, "line": 5, "column": 29 }, "modes": [ "types" ] } ] ``` One program, the build server, sent that type error to the editor, to `elements start` in the first terminal and to `elements build -json` in the second terminal. The `elements build -json` command did not run the type checker again. It asked the build server for the errors from the last build. We change `42` back to `"Ada"` and save `greet.ts`. The editor removes the underline, and `elements start` prints `State: Ok` in place of `State: Error`. We run `elements build -json` again, and the command prints `"ok": true`. ## What Most TypeScript Projects Run to Type Check In most TypeScript projects, your editor starts its own TypeScript server. Your agent runs `tsc`, `tsc` type checks every file, and your agent waits. Your bundler removes the type annotations and writes JavaScript. Your bundler does not type check your code. And your CI system runs `tsc` again. We designed Elements to have one program type check your code: the build server. ## A Type Error for the Browser Only We add a function to `greet.ts` that sorts a list of names: ```ts app/shared/greet.ts export function greet(name: string): string { return "Hello, " + name; } export let greeting = greet("Ada"); export function sortNames(names: string[]): string[] { return names.toSorted(); } ``` `sortNames` calls `toSorted`, and `toSorted` was added to JavaScript in ES2023. We save `greet.ts`. The build server type checks `greet.ts`, and `elements start` prints `State: Error` and a type error: ![The output of elements start in a terminal: State: Error, then one error at app/shared/greet.ts:8:16, Property 'toSorted' does not exist on type 'string\[\]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2023' or later. The last line of the error says browser and TS2550.](/images/01a2354d-4423-7ab6-b4ed-adc8d1eed168/4b541941aaaa4340?w=1480&h=694#light) ![The output of elements start in a terminal: State: Error, then one error at app/shared/greet.ts:8:16, Property 'toSorted' does not exist on type 'string\[\]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2023' or later. The last line of the error says browser and TS2550.](/images/01a2354d-4462-72fd-b4ec-0e97df610c81/05d4ee877f14456c?w=1480&h=694#dark) The last line of that type error says `[browser]`. `greet.ts` runs on the server and in the browser, so the build server type checks `greet.ts` twice: once against the library types that the `lib` option lists for the server, and once against the library types that the `lib` option lists for the browser. The `toSorted` call has no type error for the server. The `toSorted` call is a type error for the browser only. ## TypeScript Options in config.jsoc An Elements project has no tsconfig. You set TypeScript options in `config.jsoc`. JSOC is our config language. JSOC has comments, identifier keys, an `env()` function and build tags. The `env()` function reads an environment variable. In a JSOC file, a build tag is written in a comment on the line above a key, such as the build tag `@browser` in the comment `/** @browser */`. When the same key is written twice, once with no build tag and once under `/** @browser */`, the build server reads the value with no build tag for the server and the value under `/** @browser */` for the browser. The demo app's `config.jsoc` has this `build` block: ```jsoc config.jsoc build: { typescript: { lib: ["es2023", "dom"], /** @browser */ lib: ["es2020", "dom"], }, }, ``` The `build` block sets `lib`, the list of library types, twice. The first `lib` line has no build tag, so the first `lib` line is the server's value, ES2023. The comment above the second `lib` line has the build tag `@browser`, so the second `lib` line is the browser's value, ES2020. The ES2023 library types declare `toSorted` on arrays, and the ES2020 library types do not. `elements build -json` prints the same `toSorted` type error, and the `targets` field of that type error has one value, `"browser"`: ```json "targets": [ "browser" ], ``` We delete `sortNames` from `greet.ts` and save. `elements start` prints `State: Ok`. ## Type Errors in Reactive HTML Files The build server also type checks the TypeScript in reactive HTML files. The demo app's home page is a reactive HTML file. The home page imports `greeting` from `greet.ts`: ```ehtml app/pages/home/template.ehtml import { greeting } from "#app/shared/greet"; ``` On line 124 of the home page, the expression `{greeting}` prints the value of `greeting`: ```ehtml app/pages/home/template.ehtml <p class="home-greeting">{greeting}</p> ``` We misspell `greeting` in that expression and save the home page: ```ehtml app/pages/home/template.ehtml <p class="home-greeting">{greting}</p> ``` `elements start` prints `State: Error` and a type error that asks whether we meant `greeting`: ![The output of elements start in a terminal: State: Error, then one error at app/pages/home/template.ehtml:124:37, Cannot find name 'greting'. Did you mean 'greeting'? The error code is TS2552.](/images/01a2354d-4489-7c26-b861-ddfd217e72c4/f8e2dea40b793359?w=1480&h=624#light) ![The output of elements start in a terminal: State: Error, then one error at app/pages/home/template.ehtml:124:37, Cannot find name 'greting'. Did you mean 'greeting'? The error code is TS2552.](/images/01a2354d-44ba-79bb-a5ae-4c88f7f249db/f65f3497584b415b?w=1480&h=624#dark) The type checker that checks TypeScript files found that type error in a reactive HTML file. The editor underlines `greting`, and `elements build -json` prints the same type error. We change `greting` back to `greeting` and save the home page. `elements start` prints `State: Ok`. ## Which Files Elements Type Checks After a Save The build server keeps the parsed source files and the resolved imports in memory. The parsed files and the resolved imports in memory are the source graph. After the first build of a project, the build server parses a file again only when the text of that file has changed. After you save a file, the build server type checks the file that you changed. If the changed file has a new export, or an export with a different type, the build server also type checks the files that import the changed file. If the exported types of one of those files changed too, the build server also type checks the files that import that file. For every file that the build server does not type check in a build, the build server keeps the type errors from the last build. Changing `"Ada"` to `42` added no export to `greet.ts` and changed the type of no export: `greeting` is still a `string`. So the build server type checked one file, `greet.ts`, once for the server and once for the browser. Adding `sortNames` added an export to `greet.ts`, so the build server also type checked the home page, which imports `greet.ts`. In the video, the build server's log shows the six most recent builds of the demo app taking 3 to 6 milliseconds each. ## A Type Error in the Editor While the Build Is Still Ok The build server builds the source graph from the files on disk, and builds one more source graph for each editor that connects to the build server. Each of these source graphs is a layer. Layer 0 is the source graph that the build server builds from the text of the files on disk. The editor's layer is copy on open: for each file that you have open in your editor, the editor's layer has a copy of that file, and the copy has the text that is in your editor, including text that you have not saved. For every other file, the editor's layer has the text that is on disk. We change `"Ada"` to `42` in `greet.ts` again and do not save. The build server type checks the unsaved text of `greet.ts` in the editor's layer and finds a type error: `42` is a number, and `greet` takes a string. The build server sends that type error to the editor, so the editor underlines `42`. The terminal that runs `elements start` still shows `State: Ok`, because `greet.ts` on disk has not changed. ## The Build Adds async and await to Your JavaScript When the build writes your JavaScript, the build needs to know which function each call is calling. The type checker has already worked that out during type checking, so the build does not work it out a second time. For example, the build adds `async` and `await` to the JavaScript of a function that calls `sql()`, the function in the `@elements/app` package that runs a SQL query. The demo app has a function, `listUsers`, that calls `sql()` with no `async` and no `await`: ```ts app/shared/services/users.ts import { sql } from "@elements/app"; export interface User { id: number; name: string; } export function listUsers(): User[] { return sql<User>(` select 1 as id, 'Ada' as name `).all(); } ``` The build wrote the JavaScript below for `listUsers`. The build added `async` to the function and `await` to the call, and changed the call to `sql` into a call to `sqlAsync`. The build also wrapped the call in `app.withLoc`, which gives `sqlAsync` the location of the `sql()` call in `users.ts`, from character 142 to character 204: ```js async function listUsers() { return (await (app.withLoc({ start: 142, finish: 204 }, () => (0, app_1.sqlAsync)(` select 1 as id, 'Ada' as name `)))).all(); } ``` `sql` is a synchronous function, declared in `@elements/app`. The comment above the declaration of `sql` has a build tag, `@redirect to=sqlAsync`. `sqlAsync` is an async function. When the build wrote the JavaScript for `listUsers`, the build read from the type checker's result which function the name `sql` in the call refers to, and read the `@redirect` build tag in the comment above that function's declaration. The build then wrote a call to `sqlAsync` in place of the call to `sql`, added `await` to the call and added `async` to `listUsers`. In Elements, one program, the build server, type checks your code and writes your JavaScript. So the function that the type checker found for each call is still in memory when the build server writes your JavaScript. ## The TypeScript Compiler in Go, Compiled Into the Elements Build Server Microsoft rewrote the TypeScript compiler in Go, and Elements is written in Go too. So we built TypeScript directly into Elements: the TypeScript compiler's Go source code is compiled into the Elements build server. We changed the TypeScript compiler that is compiled into the build server: its type checker does not read files from disk and does not resolve imports. The type checker reads the files that the build server has already parsed and the imports that the build server has already resolved. The build server type checks your code for the server and for the browser at the same time, and merges the type errors into one list. We also added two languages to the TypeScript compiler in Elements: reactive HTML and JSOC. A file in either language is a module, like a TypeScript file. A reactive HTML file can import a function from a TypeScript file, and a TypeScript file can import `config.jsoc`. The type checker that checks TypeScript files also type checks the TypeScript in a reactive HTML file and every read of a value imported from a `.jsoc` file. ## Why Your Agent Does Not Run tsc in an Elements Project The build server runs a build when you save a file. When the build is done, the build server pushes the type errors to your editor. When you run the `elements build` command in your terminal, or your agent runs it, the command asks the build server for the same type errors. So your agent does not run a type checker. Your agent does not run `tsc` on every file in your project, and your agent does not have to remember which files import the file it changed. The build server has already type checked the changed file and, when the changed file has a new export or an export with a different type, the files that import the changed file. A build also runs your tests and your database migrations. The agent reads the type errors, the test errors and the migration errors with one command: ```bash Terminal elements build -json ``` The build server answers that command in microseconds.

TypeScript is built directly into Elements. It type checks your code for the server and the browser. A new source graph with layers makes that really fast. Your editor, terminal and agent get the same errors, in microseconds. This function, greet, takes a string. I pass it a number. That's a type error, and my editor underlines the number. I save, and the Elements build server runs a build. The build server type checks the file and reports the type error in my terminal. An agent runs this command to read the errors from the last build. The build server returns the same error, in microseconds. One program, the build server, sent that error to my editor, my terminal and the agent's command.

The agent's command didn't run the type checker again. It asked the build server for the errors from the last build. I change the number back to a name, and save. My editor removes the underline. The build is ok, and the agent's command returns ok. In most TypeScript projects, your editor starts its own TypeScript server. Your agent runs tsc. It type checks every file, and your agent waits. Your bundler removes the type annotations and writes JavaScript. It doesn't type check your code at all. And your CI system runs tsc again. I designed Elements to have one program type check your code: the build server. I add a function that sorts a list of names. It calls toSorted, which was added to JavaScript in ES2023.

I save. The build server reports a type error. toSorted does not exist on a string array. The last line of that error says browser. This file runs on the server and in the browser. So Elements type checks it twice: once for the server, and once for the browser. The toSorted call has no error for the server. It's an error for the browser only. An Elements project has no tsconfig. You set TypeScript options in config.jsoc. JSOC is our config language. It has comments, identifier keys, an env function and build tags. This file sets lib, the list of library types, twice. The first lib line is the server's value, ES2023. The comment above the second lib line has a build tag: at browser.

So the second lib line is the browser's value, ES2020. So the server's lib declares toSorted, and the browser's lib does not. The agent's command prints the same error, and the field named targets has one value: browser. I delete the function, and save. The build is ok. The build server also type checks the TypeScript in our reactive HTML files. This file is the home page, and this expression prints the greeting. I misspell greeting, and save. The error asks: did you mean greeting? The same type checker that checks TypeScript files found that error, in a reactive HTML file. My editor underlines the misspelled name. My terminal and the agent's command print the same error. I fix the spelling, and save.

The build is ok. The build server reads each source file once, parses it once and resolves its imports. It keeps the parsed files and resolved imports in memory. That's the source graph. In this tree, a parent file imports the child files under it. After a save, Elements type checks the file that you changed. If that file has a new export, or an export with a different type, Elements also type checks its parent files. It stops at the first parent file whose exports didn't change. For every other file, Elements keeps the errors from the last build. So a build after a save takes a few milliseconds. The source graph has layers. Layer zero is the files on disk.

Your editor gets its own layer, and it's copy on open. A file you have open gets a copy with your unsaved text. Every other file comes from the disk layer. So I pass a number to greet again, and I don't save. My editor underlines the number, because the editor's layer has my unsaved text. The build is still ok, because the file on disk hasn't changed. We use the symbols from type checking to make decisions when we transform your files during emit. One example is the automatic async await transform. This is my function, listUsers. It calls sql, with no async and no await. This is the JavaScript the build wrote. It has async and await added automatically.

During emit, the build looked up the symbol for sql, saw that sql has an async version, and added async and await automatically. That's a major benefit of sharing one source graph. The symbols from type checking are available during bundling and emit. Microsoft rewrote the TypeScript compiler in Go, and Elements is written in Go too. So we built TypeScript directly into Elements. We made a few changes. The type checker uses our own file watcher, multi-dimensional source graph, and change detection algorithms. It checks your code for the server and for the browser concurrently, and merges the errors into one list. And we added two new languages to TypeScript: reactive HTML and JSOC. A file in either language is a module, like a TypeScript file.

A page can import a function from TypeScript. TypeScript can import your config. And one type checker checks all three languages together, across those imports. The build is triggered automatically when you save a file. When it's done, the errors are pushed to your editor, and they're available on request to other tools, like your terminal and your agents. So your agent doesn't run a type checker at all. It doesn't run tsc on every file in your project. It doesn't have to remember which files depend on the file it changed. The build has already type checked that file and its parent files. The agent reads any type errors, along with the rest of the build errors, with the elements build command.

That command answers in microseconds.

5 comments · 5 replies

How do I turn on strict mode or set compiler options like noUnusedLocals without a tsconfig.json?

Strict mode is already on. Unless config.jsoc, the project's config file, sets strict to false, the build server type checks your code with strict on.

Under build.typescript in config.jsoc, you set strict and each option that strict turns on, for example strictNullChecks and noImplicitAny. You set these compiler options under build.typescript too: exactOptionalPropertyTypes, noImplicitOverride, noImplicitReturns, noFallthroughCasesInSwitch, noUnusedLocals, noUnusedParameters, noUncheckedIndexedAccess, noPropertyAccessFromIndexSignature, allowUnreachableCode, allowUnusedLabels, target, lib, types, useDefineForClassFields and moduleDetection. Each option has the name it has in a tsconfig's compilerOptions:

config.jsoc
build: { typescript: { noUnusedLocals: true, noUncheckedIndexedAccess: true, }, },

The build server does not read a tsconfig.json.

Do I still need to run tsc --noEmit in CI or before a deploy?

No. I designed Elements to have one program type check your code: the build server. The build server type checks a file when you save it, and a type error is a build error.

A deploy does not release code that has a type error. Each server that you deploy to builds the new release, and that build type checks your code. If the build has a type error, elements deploy prints the type error and stops, and the release that was running keeps serving requests.

In a CI script, run elements build -json. The command starts the build server if no build server is running, prints each type error with its file, line and column, and exits with code 1 when the build has an error.

Do I still get TypeScript autocomplete, go to definition and rename in VS Code? Which language server does my editor connect to in an Elements project?

Yes. Your editor connects to the Elements build server, the program that type checks your code when you save a file. The build server implements the Language Server Protocol. The build server answers your editor's requests for completions, go to definition, find references and rename in .ts files and in reactive HTML files. In a .jsoc file, the build server answers your editor's requests for go to definition, find references and rename.

Installing Elements installs the Elements extension for VS Code and Cursor, and the Elements plugin for Vim and Neovim. In VS Code, the extension turns off VS Code's built-in TypeScript validation and suggestions for the project's workspace, so VS Code shows each type error and each completion once, from the build server.

Does Claude Code get TypeScript type errors from a language server (LSP) in an Elements project?

@chris editedAuthor

Yes. Claude Code reads type errors from the Elements build server in two ways: it runs elements build -json, and it receives type errors over the Language Server Protocol (LSP).

elements build -json prints the errors of the last build as JSON, and each type error has its file, line and column. The AGENTS.md file that elements create writes in a new app tells the agent to run elements build -json after it saves each small piece of work, such as a route or a section of a page. Codex and Cursor read type errors with the same command. An agent does not need a language server to read type errors in an Elements project.

Claude Code also receives type errors over LSP. When Claude Code is on your computer, installing Elements registers a plugin named elements with Claude Code. When you create an app with elements create, the command turns that plugin on in the .claude/settings.json of the new project. The plugin runs elements-lsp, a program that connects Claude Code to the project's build server over LSP.

When Claude Code saves an edit to a .ts, .js, .ehtml, .jsoc or .css file, the build server runs a build and sends that build's type errors to Claude Code. Claude Code does not request the errors and does not run a command. If the edit causes a type error in a second file, the build server sends that error too. Claude Code's LSP tool can also go to the definition of a symbol, find the references to that symbol and show the type of that symbol.

Does Elements type check the attributes I set on a template in a reactive HTML file?

@chris editedAuthor

Yes. In a reactive HTML file, a template is a top-level tag whose name starts with a capital letter, such as <Greet>. A template declares its attributes in () inside its opening tag, and each attribute has a TypeScript type, like a function parameter: <Greet (name: string)>.

The build server type checks every attribute that you set on a template. <Greet name={42} /> is a type error at the line and column of 42: Argument of type 'number' is not assignable to parameter of type 'string'. <Greet /> is a build error too: Template 'Greet' is missing required attribute 'name'.

Add a comment