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 looks a little different and uses Ctrl for ⌘, but everything works the same way.

Esc

Type to search every docs page.