Typescript基础语法

青云 2024-5-28 1917

1.基础类型

1.布尔值
let isDone:boolean = true
2.数字

所有数字都是浮点数,类型为number,支持十进制、二进制、八进制、十六进制。打印结果会转为十进制

let a:number = 10
let b:number = 0b1101
let c:number = 0o376
let d:number = 0x73e
3.字符串

string表示字符串类型,使用单引号‘或者双引号”表示

let str:string = 'bac'
let str1:string = "abc"
4.数组

使用元素类型后面接【】如:number[]

使用数组泛型Array<元素类型> 如:Array<number>

let arr:number[] = [1,2,3]
let arr1:Array<number> = [2,3,4]
5.元组

表示一个已知元素的数量和元素的类型的数组,各个元素可不同

let a:[string,number] = ['abc',123]
6.枚举

枚举使用enum对数据类型的补充

enum Color{Red,Green,Blue}
let a:Color = Color.Red
7.unknown

不清楚类型的可用unknown类型指定

let a:unknown = 4
a = 'abc'
a = [1,2,3]
8.void

函数没有返回值,使用void

function abc():void{ }
9.null和undefined
let a:undefined = undefined
let b:null = null
10.联合类型

表示可以为多个类型中的一种

let a:string|number = 'abc'
a = 10

2.条件语句

1.if..else
let a:number = 10
if(a>5){
	...
}else if(a>6){
	...
}else{
}
2.switch
let a:string = "A"
switch(a){
	case "A":{
	...
	break; 
	}
	case "B":{
	...
	break; 
	}	
	default:{
	...
	break;
	}
}

3.函数

1.定义

函数是一组一起执行一个任务的语句,函数声明要告诉编译器函数的名称、返回类型和参数。TypeScript可以创建有名字的函数和匿名函数,其创建方法如下:


// 有名函数
function add(x, y) {
  return x + y;
}

// 匿名函数
let myAdd = function (x, y) {
  return x + y;
};
// 有名函数:给变量设置为number类型
function add(x: number, y: number): number {
  return x + y;
}

// 匿名函数:给变量设置为number类型
let myAdd = function (x: number, y: number): number {
  return x + y;
};
可选参数
// 有名函数:给变量设置为number类型
function add(x: number, y: number): number {
  return x + y;
}

// 匿名函数:给变量设置为number类型
let myAdd = function (x: number, y: number): number {
  return x + y;
};
剩余参数
function getEmployeeName(firstName: string, ...restOfName: string[]) {
  return firstName + ' ' + restOfName.join(' ');
}

let employeeName = getEmployeeName('Joseph', 'Samuel', 'Lucas', 'MacKinzie');
//箭头函数
( [param1, parma2,…param n] )=> {
    // 代码块
}

let arrowFun = ( [param1, parma2,…param n] )=> {
    // 代码块
}
arrowFun(param1, parma2,…param n)

4.类

1.类的定义

TypeScript支持基于类的面向对象的编程方式,定义类的关键字为 class,后面紧跟类名。类描述了所创建的对象共同的属性和方法。

class Person {
  private name: string
  private age: number

  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }

  public getPersonInfo(): string {
    return `My name is ${this.name} and age is ${this.age}`;
  }
}
let person1 = new Person('Jacky', 18);
person1.getPersonInfo();
2.继承

继承就是子类继承父类的特征和行为,使得子类具有父类相同的行为。TypeScript中允许使用继承来扩展现有的类,对应的关键字为extends。

class Employee extends Person {
  private department: string

  constructor(name: string, age: number, department: string) {
    super(name, age);
    this.department = department;
  }

  public getEmployeeInfo(): string {
    return this.getPersonInfo() + ` and work in ${this.department}`;
  }
}

let person2 = new Employee('Tom', 28, 'HuaWei');
person2.getPersonInfo();
person2.getEmployeeInfo();
在TypeScript中,有public、private、protected修饰符

5.模块

随着应用越来越大,通常要将代码拆分成多个文件,即所谓的模块(module)。模块可以相互加载,并可以使用特殊的指令 export 和 import 来交换功能,从另一个模块调用一个模块的函数。

1.导出

任何声明(比如变量,函数,类,类型别名或接口)都能够通过添加export关键字来导出,例如我们要把NewsData这个类导出,代码示意如下:

export class NewsData {
  title: string;
  content: string;
  imagesUrl: Array<NewsFile>;
  source: string;

  constructor(title: string, content: string, imagesUrl: Array<NewsFile>, source: string) {
    this.title = title;
    this.content = content;
    this.imagesUrl = imagesUrl;
    this.source = source;
  }
}
2.导入

模块的导入操作与导出一样简单。 可以使用以下 import形式之一来导入其它模块中的导出内容。

import { NewsData } from '../common/bean/NewsData';

6.迭代器

当一个对象实现了Symbol.iterator属性时,我们认为它是可迭代的。一些内置的类型如Array,Map,Set,String,Int32Array,Uint32Array等都具有可迭代性。

1.for..of

for..of会遍历可迭代的对象,调用对象上的Symbol.iterator方法。 下面是在数组上使用for..of的简单例子:

let someArray = [1, "string", false];

for (let entry of someArray) {
    console.log(entry); // 1, "string", false
}

2.for..in语句

for..of和for..in均可迭代一个列表,但是用于迭代的值却不同:for..in迭代的是对象的键,而for..of则迭代的是对象的值。

let list = [4, 5, 6];

for (let i in list) {
    console.log(i); // "0", "1", "2",
}

for (let i of list) {
    console.log(i); // "4", "5", "6"
}
最新回复 (22)
全部楼主
返回
发新帖