MenuGuides / Try out TypeScript types

Docs / Guides

Try out TypeScript types

Switch a Snippet to TypeScript, read the types as you write, and see what a type error does to a Run.

On this page

TypeScript needs no setup in JSpark: no install, no config file. Each Snippet has a Language, JavaScript or TypeScript.

Switch to TypeScript

A new Snippet is JavaScript. JavaScript runs as JavaScript, so a type in it is a mistake, and the Run stops with a SyntaxError.

  1. Click the Language button in the footer. It shows the Language's glyph, or its name.
  2. Choose TypeScript: Types are checked as you type.

The underlines go. With Run as you type on, the Snippet runs again at once, as TypeScript.

The types were errors in JavaScript; as TypeScript, the same code runs and prints 5.

To make every new Snippet TypeScript, open Settings → Running → Language for new Snippets (⌘, or Ctrl+,). Snippets you already have keep their Language.

Read a type

Rest the pointer on a name. A box shows the type TypeScript worked out for it, such as a function's parameters and what it returns. You don't have to write the type yourself for it to show.

Hovering price shows its type; the string passed where a number is expected is underlined.

To turn the boxes off, switch off Settings → Editor → Show types on hover.

Fix a type error

  1. Find the underline. Hover it to read the error.
  2. Change the code so the types match. The underline goes as you type.

By default, a type error does not stop the Run: the code is underlined and runs anyway. In the example above, price("12.50") still runs, and prints $0.13.

To stop a Run while there is a type error, switch on Settings → Running → Don’t run with type errors. Then the errors are printed in the console and nothing runs until you fix them. See My TypeScript did not run because of a type error.

The pictures show JSpark on a Mac. On Windows, the window buttons are at the top right, the menus are in theMenu button at the left end of the tabs, and shortcuts use Ctrl where a Mac uses ⌘. The rest works the same way.

Esc

Type to search every docs page.

JSpark is downloading

When you open JSpark-Setup.exe, Windows may stop it once, because the installer isn’t code-signed yet. If the file came from jspark.app:

A blue Windows box, “Windows protected your PC”: Microsoft Defender SmartScreen prevented an unrecognised app from starting. Under it, the link More info, and the button Don’t run.

1Click More info.

The same box after More info: Application JSpark-Setup-1.4.0.exe, Publisher Unknown publisher, and the buttons Run anyway and Don’t run.

2Then click Run anyway.

You see this only once: updates install inside the app. More in Install JSpark.