以下是一些 TypeScript 的基础语法示例:
1. 变量声明
```typescript
let num: number = 10
let str: string = "hello"
let bool: boolean = true
```
2. 函数声明
```typescript
function add(a: number, b: number): number {
return a + b
}
let result: number = add(5, 10)
console.log(result)
```
3. 接口声明
```typescript
interface Person {
name: string
age: number
}
let person: Person = {
name: "John",
age: 30
}
```
4. 类声明
```typescript
class Animal {
private name: string
constructor(name: string) {
this.name = name
}
public getName(): string {
return this.name
}
}
let animal: Animal = new Animal("dog")
console.log(animal.getName())
```
5. 泛型声明
```typescript
function identity<T>(arg: T): T {
return arg
}
let output: string = identity<string>("hello")
console.log(output)
```
6. 类型别名声明
```typescript
type Point = [number, number]
let point: Point = [10, 20]
console.log(point)
```