TypeScript Basic Structure
The basic structure of a TypeScript program can be divided into several parts, each with a specific role.
The following are common components of a TypeScript program:
- Declarations: including type declarations, interface declarations, etc.
- Variable Declaration: including
let,constandvartheir use. - Function declaration: including regular functions and arrow functions.
- Class Declaration: used to define classes and their members.
- Interfaces and type aliases: describe the structure of types.
- Modularity: Through
importandexportOrganize code. - Type assertions: forced type conversion.
- Generics: makes code more reusable.
- Notes: increases code readability.
- Type Inference: automatically infers types.
- Type guards: narrows the type range.
- Asynchronous programming: Supports
async/await。 - Error handling: Through
try/catchPerform error capturing.
The above parts together form the basic structure of a TypeScript program and provide developers with powerful type checking, code structure, and maintainability.
1. Declaration section (Declarations)
**Type declarations:** TypeScript is a statically typed language; type declarations can be used to define the types of variables, functions, classes, etc. Type declarations can make code more maintainable and readable.
Example
let name
:
string
=
"Alice"
;
let age
:
number
=
30
;
**Interface declarations:** used to define the structure of an object, including its properties and methods.
Example
interface
Person
{
name
:
string
;
age
:
number
;
}
2. Variable Declarations (Variable Declarations)
In TypeScript, let, const, and var can be used to declare variables. let and const are recommended; the use of var is no longer recommended.
Example
let age
:
number
=
25
;
const
pi
:
number
=
3.14
;
3. Function Declarations (Function Declarations)
Function declarations: TypeScript allows declaring functions with type annotations, including parameter types and return types.
Example
function
greet
(
name
:
string
)
:
string
{
return
"Hello, "
name
;
}
**Arrow functions:** TypeScript also supports ES6 arrow functions, which use a concise syntax to declare functions.
Example
const
greet
=
(
name
:
string
)
:
string
=>
"Hello, "
name
;
4. Class Declarations (Class Declarations)
TypeScript provides support for object-oriented programming, allowing the definition of classes and their methods and properties.
Example
class
Person
{
name
:
string
;
age
:
number
;
constructor
(
name
:
string
,
age
:
number
)
{
this
.
name
=
name
;
this
.
age
=
age
;
}
greet
(
)
{
return
`Hello
,
my name is $
{
this
.
name
}
`
;
}
}
5. Interfaces & Type Aliases (Interfaces & Type Aliases)
Interface (Interface): used to describe the shape of an object; interfaces can be inherited and extended.
Example
interface
Animal
{
name
:
string
;
sound
:
string
;
makeSound
(
)
:
void
;
}
**Type Alias (Type Alias):** allows defining aliases for object types, union types, intersection types, etc.
Example
type ID
=
string
|
number
;
6. Modules and Imports/Exports (Modules & Imports/Exports)
TypeScript supports modular programming; import and export can be used to organize code.
** Export:**
Example
export
class
Person
{
constructor
(
public name
:
string
)
{
}
}
Import:
Example
import
{
Person
}
from
'./person'
;
7. Type Assertions (Type Assertions)
In some cases, TypeScript cannot infer the exact type of a variable; developers can use type assertions to force a specific type.
Example
let value
:
any
=
"hello"
;
let strLength
:
number
=
(
value as string
)
.
length
;
8. Generics (Generics)
Generics allow defining functions, interfaces, or classes without specifying a concrete type, instead using placeholders that users fill in with a concrete type when using them. Generics can increase code reusability and type safety.
Example
function
identity
<
T
(
arg:
T
)
:
T
{
return
arg
;
}
9. Comments (Comments)
Comments in a TypeScript program are used to explain the purpose and ideas of the code, increasing code readability.
Single-line comment:
Example
// This is a single-line comment
Multi-line comment:
Example
/* This is a
multi-line comment */
10. Type Inference (Type Inference)
TypeScript automatically infers the type of a variable in some cases. For example, when a variable is declared and assigned a value, TypeScript infers the variable's type.
Example
let num
=
10
;
// TypeScript infers num as type number
11. Type Guards (Type Guards)
TypeScript provides type guards (such as typeof and instanceof) to narrow the type range of a variable at runtime.
Example
function
isString
(
value
:
any
)
:
value is string
{
return
typeof
value
===
'string'
;
}
12. Asynchronous Programming (Asynchronous Programming)
TypeScript fully supports asynchronous programming; the async/await syntax can be used to handle asynchronous operations.
Example
async
function
fetchData
(
)
:
Promise
<
string
{
const
response
=await fetch
(
"https://example.com"
)
;
const
data
=
await response.
text
(
)
;
return
data
;
}
13. Error Handling (Error Handling)
TypeScript allows using try/catch blocks for error handling, and types can also be used to describe error types.
Example
try
{
throw
new
Error
(
"Something went wrong"
)
;
}
catch
(
error
)
{
if
(
error
instanceof
Error
)
{
console.
error
(
error.
message
)
;
}
}