TypeScriptLesson 1 of 16
What is TypeScript? Your first program
TypeScript is JavaScript with types added. The compiler checks them before the code runs, then turns everything into plain JavaScript.
Key points
- Almost any JavaScript is already valid TypeScript, so you are not starting from zero.
- A type goes after a colon: const title: string = "Hi".
- Type errors show up in the editor before the program runs.
- Types are erased when compiling: browsers and Node.js run plain JavaScript.
Example
const language: string = "TypeScript";const lessons: number = 16;// const broken: number = "16"; // error: string is not assignable to number console.log(`${language}: ${lessons} lessons`);The : string and : number annotations tell the compiler what each constant holds. Uncomment the broken line and TypeScript reports that a string cannot go into a number.
Task
How to use the editor⌘ Enter or Ctrl Enter runs the code, ⇧ ⌘ Enter checks the task.Tab and Shift Tab indent the line or the selection; Esc, then Tab leaves the editor.The check compares what the program printed with the task; spaces at line ends do not matter.After two failed checks you may open the solution. Your code is kept in this browser.Give the constant greeting the string type, store the text "Hello, TypeScript!" in it and print it. The program must print exactly Hello, TypeScript!
Output
What your program prints appears here.