TypeScript Basic Structure
The basic structure of a TypeScript program can be divided into several parts, each with a specific purpose.
The following are the common components of a TypeScript program:
- Declarations: including type declarations, interface declarations, etc.
- Variable Declarations: including
let,constandvarthe use of. - Function Declarations: including regular functions and arrow functions.
- Class Declarations: used to define classes and their members.
- Interfaces & Type Aliases: describe the structure of types.
- Modularization: through
importandexportorganize code. - Type Assertions: forced type conversion.
- Generics: make code more reusable.
- Comments: increase code readability.
- Type Inference: automatically infers types.
- Type Guards: narrow the type scope.
- Asynchronous Programming: supports
async/await。 - Error Handling: through
try/catchto catch errors.
The above parts together form the basic structure of a TypeScript program, providing developers with powerful type checking, code structure, and maintainability.
1. Declarations
Type declarations:TypeScript is a statically typed language that uses type declarations to define the types of variables, functions, classes, and more. Type declarations help make code more maintainable and readable.
Example
let age: number = 30;
Interface declarations:Used to define the structure of an object, including its properties and methods.
Example
name: string;
age: number;
}
2. Variable Declarations
In TypeScript, you can use let, const, and var to declare variables. It is recommended to use let and const; var is no longer recommended.
Example
const pi: number = 3.14;
3. Function Declarations
Function declarations: TypeScript allows declaring functions with type annotations, including parameter types and return types.
Example
return "Hello, " + name;
}
Arrow functions:TypeScript also supports ES6 arrow functions, using concise syntax to declare functions.
Example
4. Class Declarations
TypeScript provides support for object-oriented programming, allowing you to define classes and their methods and properties.
Example
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
Interface: used to describe the shape of an object; interfaces can be inherited and extended.
Example
name: string;
sound: string;
makeSound(): void;
}
Type Alias:Allows defining aliases for object types, union types, intersection types, and more.
Example
6. Modules & Imports/Exports
TypeScript supports modular programming; you can use import and export to organize code.
Export:
Example
constructor(public name: string) {}
}
Import:
Example
7. Type Assertions
In some cases, TypeScript cannot infer the exact type of a variable; developers can use type assertions to explicitly specify the type.
Example
let strLength: number = (value as string).length;
8. Generics
Generics allow you to avoid specifying concrete types when defining functions, interfaces, or classes, instead using placeholders that let users pass in specific types when using them. Generics increase code reusability and type safety.
Example
return arg;
}
9. Comments
Comments are used in TypeScript programs to explain the purpose and logic of code, increasing readability.
Single-line comments:
Example
Multi-line comments:
Example
multi-line comment */
10. Type Inference
TypeScript automatically infers the type of a variable in certain situations. For example, when declaring a variable and assigning a value, TypeScript infers the variable's type.
Example
11. Type Guards
TypeScript provides type guards (such as typeof and instanceof) to narrow the type range of variables at runtime.
Example
return typeof value === 'string';
}
12. Asynchronous Programming
TypeScript fully supports asynchronous programming; you can use async/await syntax to handle asynchronous operations.
Example
const response = await fetch("https://example.com");
const data = await response.text();
return data;
}
13. Error Handling
TypeScript allows error handling with try/catch blocks, and you can also use types to describe error types.
Example
throw new Error("Something went wrong");
} catch (error) {
if (error instanceof Error) {
console.error(error.message);
}
}