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.
- Click the Language button in the footer. It shows the Language's glyph, or its name.
- 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.
- Type aliases can only be used in TypeScript files.
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.
- Argument of type 'string' is not assignable to parameter of type 'number'.
To turn the boxes off, switch off Settings → Editor → Show types on hover.
Fix a type error
- Find the underline. Hover it to read the error.
- 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.