Ts Basic Syntax
TypeScript programs consist of the following parts:
* Modules
* Functions
* Variables
* Statements and expressions
* Comments
### The First TypeScript Program
We can use the following TypeScript program to output "Hello World":
## Tutorial.ts file code:
const hello : string = "Hello World!"console.log(hello)
[Try it Β»](#)
The above code is first compiled using the **tsc** command:
tsc Tutorial.ts
We get the following js code:
## Tutorial.js file code:
var hello = "Hello World!"; console.log(hello);
Finally we use the node command to execute this js code.
$ node Tutorial.js Hello World
The entire process is shown in the figure below:
!(#)
We can compile multiple ts files at the same time:
tsc file1.ts file2.ts file3.ts
Common tsc compilation parameters are shown in the table below:
| No. | Compilation Parameter Description |
| --- | --- |
| 1. | **--help** Display help information |
| 2. | **--module** Load extension module |
| 3. | **--target** Set ECMA version |
| 4. | **--declaration** Additionally generate a .d.ts extension file. tsc ts-hw.ts --declaration The above command will generate ts-hw.d.ts and ts-hw.js two files. |
| 5. | **--removeComments** Remove comments from the file |
| 6. | **--out** Compile multiple files and merge into one output file |
| 7. | **--sourcemap** Generate a sourcemap (.map) file. sourcemap is an information file that stores the mapping between source code and compiled code positions. |
| 8. | **--module noImplicitAny** Report error when there is implicit any type in expressions and declarations |
| 9. | **--watch** Run the compiler in watch mode. It will watch output files and recompile them when they change. |
* * *
## TypeScript Reserved Keywords
TypeScript reserved keywords are shown in the table below:
| Keyword | Description |
| --- | --- |
| `abstract` | Used to define abstract classes or abstract methods. |
| `any` | Represents any type, disables type checking. |
| `as` | Type assertion, used to convert one type to another. |
| `await` | Used in async functions to pause code execution until the Promise resolves. |
| `boolean` | Represents boolean type. |
| `break` | Exit loop or switch statement. |
| `case` | Used for branches in switch statements. |
| `catch` | Used to catch exceptions. |
| `class` | Used to define a class. |
| `const` | Define constant variables. |
| `continue` | Skip current loop and continue to next iteration. |
| `debugger` | Start debugger and pause code execution. |
| `declare` | Declare a variable or module, usually used in type declaration files. |
| `default` | Define default branch in switch statement. |
| `delete` | Delete property of object or element of array. |
| `do` | Used in `do...while` loop. |
| `else` | Define else part in conditional statement. |
| `enum` | Define enumeration type. |
| `export` | Used to export variables, functions or classes from a module. |
| `extends` | Used for class inheritance, indicates that a class inherits from another class. |
| `false` | Boolean value `false`. |
| `finally` | Define the finally execution block in `try...catch` statement. |
| `for` | Used in `for` loop. |
| `from` | Used in module import statements to specify the source of the module. |
| `function` | Define a function. |
| `get` | Used for getter method of an object. |
| `if` | Used for conditional judgment. |
| `implements` | Used for a class to implement an interface. |
| `import` | Used to import content from a module. |
| `in` | Used to check if an object contains a specified property, or used in `for...in` loop. |
| `infer` | Used in conditional types to infer types. |
| `instanceof` | Check if an object is an instance of a specified class. |
| `interface` | Used to define an interface. |
| `let` | Define block-scoped variables. |
| `module` | Define a module (used in earlier TypeScript versions). |
| `namespace` | Define a namespace (used in earlier TypeScript versions). |
| `new` | Create an instance of a class. |
| `null` | Represents null value. |
| `number` | Represents number type. |
| `object` | Represents non-primitive type. |
| `of` | Used in `for...of` loop. |
| `package` | Used in module system to identify a package. |
| `private` | Used as access modifier for class members, indicates private. |
| `protected` | Used as access modifier for class members, indicates protected. |
| `public` | Used as access modifier for class members, indicates public. |
| `readonly` | Represents read-only property. |
| `require` | Used to import CommonJS modules. |
| `return` | Exit function and can return a value. |
| `set` | Used for setter method of an object. |
| `string` | Represents string type. |
| `super` | Used to call parent class's method or constructor. |
| `switch` | Used in switch statement. |
| `symbol` | Represents symbol type. |
| `this` | Reference to current class or object's instance. |
| `throw` | Throw an exception. |
| `try` | Used in exception handling statement `try...catch`. |
| `true` | Boolean value `true`. |
| `type` | Used to define type aliases. |
| `typeof` | Get the type of a variable or expression. |
| `undefined` | Represents undefined value. |
| `
YouTip