TypeScript Basic Syntax
A TypeScript program consists of the following parts:
- module
- function
- Variables
- Statements and Expressions
- Notes
Your First TypeScript Program
We can use the following TypeScript program to output "Hello World":
Code in the Runoob.ts file:
const
hello
:
string
=
"
Hello World!
"
console
.
log
(
hello
)
Try it »
The code above is first compiled via the tsc command:
tsc Runoob.ts
producing the following js code:
Code in the Runoob.js file:
var
hello
=
"
Hello World!
"
;
console
.
log
(
hello
)
;
Finally, we use the node command to execute this js code.
$ node Runoob.js
Hello World
The whole process is shown in the figure below:

We can compile multiple ts files at the same time:
tsc file1.ts file2.ts file3.ts
Commonly used tsc compilation options are shown in the table below:
| No. | Compilation option description |
|---|---|
| 1. |
--help Display help information |
| 2. |
--module Load external modules |
| 3. |
--target Set the ECMA version |
| 4. |
--declaration Additionally generate a file with the .d.ts extension. |
tsc ts-hw.ts --declaration
The above command generates two files, ts-hw.d.ts and ts-hw.js.
| | 5. |--removeComments
Remove comments from the file
| | 6. |--out
Compile multiple files and merge them into one output file
| | 7. |--sourcemap
Generate a sourcemap (.map) file.
A sourcemap is an information file that stores the mapping between positions in the source code and the compiled code.
| | 8. |--module noImplicitAny
Report errors when implicit any types occur in expressions and declarations
| | 9. |--watch
Run the compiler in watch mode. It monitors the output files and recompiles them when they change.
|TypeScript reserved keywords
TypeScript reserved keywords are listed in the table below:
| The keyword | Description |
|---|---|
abstract |
Used to define an abstract class or abstract method. |
any |
Represents any type and 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 is resolved. |
boolean |
Represents the boolean type. |
break |
Exits a loop or a switch statement. |
case |
Used for a branch in a switch statement. |
catch |
Used to catch exceptions. |
class |
Used to define a class. |
const |
Defines a constant variable. |
continue |
Skips the current iteration and continues with the next one. |
debugger |
Starts the debugger and pauses code execution. |
declare |
Declares a variable or module, commonly used in type declaration files. |
default |
Defines the default branch of a switch statement. |
delete |
Deletes a property of an object or an element of an array. |
do |
Used for do...while loops. |
else |
Defines the else part of a conditional statement. |
enum |
Defines an enum type. |
export |
Used to export variables, functions, or classes from a module. |
extends |
Used for class inheritance, indicating that a class inherits from another class. |
false |
Boolean false。 |
finally |
define try...catch The final block of code executed in a statement. |
for |
Used for for loops. |
from |
Used in a module import statement to specify the source of the module. |
function |
to define a function. |
get |
Used for an object's getter method. |
if |
Used for conditional evaluation. |
implements |
Used for a class implementing an interface. |
import |
Used to import content from a module. |
in |
Used to check whether an object contains a specified property, or for for...in loops. |
infer |
inferring types in conditional types. |
instanceof |
Checks whether an object is an instance of a specified class. |
interface |
Used to define an interface. |
let |
Defines a block-scoped variable. |
module |
Defines a module (used in earlier versions of TypeScript). |
namespace |
Defines a namespace (used in earlier versions of TypeScript). |
new |
Creates an instance of a class. |
null |
Represents a null value. |
number |
Represents the number type. |
object |
Represents a non-primitive type. |
of |
Used for for...of loops. |
package |
Used in the module system to identify a package. |
private |
An access modifier for class members, indicating private. |
protected |
An access modifier for class members, indicating protected. |
public |
An access modifier for class members, indicating public. |
readonly |
Represents a readonly property. |
require |
Used to import CommonJS modules. |
return |
Exits a function and can return a value. |
set |
Used for an object's setter method. |
string |
Represents the string type. |
super |
Used to call a method or constructor of the parent class. |
switch |
Used in a switch statement. |
symbol |
Represents the symbol type. |
this |
Refers to the instance of the current class or object. |
throw |
Throws an exception. |
try |
Used in exception handling statements try...catch。 |
true |
Boolean true。 |
type |
Used to define a type alias. |
typeof |
Obtains the type of a variable or expression. |
undefined |
Represents an undefined value. |
unique |
Used for symbol A unique identifier for a type. |
var |
Used to declare a variable (no longer recommended). |
void |
Represents a type with no return value. |
while |
Used for while loops. |
with |
Used to create a scope in which references to an object can be omitted (not recommended). |
yield |
Used in generator functions to pause and resume execution. |
Whitespace and line breaks
TypeScript ignores spaces, tabs, and line breaks in a program.
Spaces and tabs are usually used to indent code, making it easier to read and understand.
TypeScript is case-sensitive
TypeScript distinguishes between uppercase and lowercase characters.
Semicolons are optional
Each line of instruction is a statement; you may use a semicolon or not. Semicolons are optional in TypeScript, but recommended.
The following code is all valid:
console.log("Runoob")
console.log("Google");
If statements are written on the same line, semicolons must be used to separate them, otherwise an error will occur, for example:
console.log("Runoob");console.log("Google");
TypeScript comments
Commenting is a good habit. Although many programmers dislike comments, it is still recommended that you add a text description to each piece of code.
Comments can improve the readability of a program.
Comments can contain information about the program, such as the author of the code and descriptions of functions.
The compiler ignores comments.
TypeScript supports two types of comments
-
Single-line comments ( // ) − All text after // is comment content.
-
Multi-line comments (/* */) − This type of comment can span multiple lines.
Comment example:
// 这是一个单行注释
/*
这是一个多行注释
这是一个多行注释
这是一个多行注释
*/
TypeScript and object-oriented programming
Object-oriented programming is an approach to understanding and abstracting the real world.
TypeScript is an object-oriented programming language.
Object-oriented programming has two main concepts: objects and classes.
- object: An object is an instance of a class (Objects are not about finding a girlfriend), having state and behavior. For example, a dog is an object; its state includes: color, name, breed; its behaviors include: wagging tail, barking, eating, etc.
- class: A class is a template that describes the behavior and state of a class of objects.
- Method: A method is the implementation step of a class's operation.
In the figure below girl、boy as a class, and each specific person is an object of that class:

Example of object-oriented programming in TypeScript:
class
Site
{
name
(
)
:
void
{
console
.
log
(
"
Runoob
"
)
}
}
var
obj
=
new
Site
(
)
;
obj
.
name
(
)
;
The example above defines a class Site with a method name(), which outputs the string Runoob to the terminal.
The new keyword creates an object of the class, and that object calls the method name().
The JavaScript code generated after compilation is as follows:
var
Site
=
/*
- @class
*/
(
function
(
)
{
function
Site
(
)
{
}
Site
.
prototype
.
name
=
function
(
)
{
console
.
log
(
"
Runoob
"
)
;
}
;
return
Site
;
}
(
)
)
;
var
obj
=
new
Site
(
)
;
obj
.
name
(
)
;
Executing the above JavaScript code produces the following output:
Runoob