TS快速学习
1.基础类型
布尔值
TypeScript 中可以使用boolean来表示这个变量是布尔值,可以赋值为true/false
let flag: boolean = false ;
数字
TypeScript 里的所有数字都是浮点数,这些浮点数的类型是number,除了十进制,还支持二进制,八进制,十六进制
let num1: number = 2003
let num2: number = 0b111110011
let num3: number = 0o3747
let num4: number = 0x7e7
字符串
TypeScript 里使用string 表示文本数据类型,可以使用双引号"" 或者单引号'' 表示字符串
let name: string = '张三'
name = '王五'
name = "赵六"
数组
TypeScript 里支持以下两种方式声明数组:第一种,可以在元素类型后面接上 [ ] ,表示由此类型元素组成的一个数组;第二种方式是使用数组泛型,Array<元素类型>
let list1: number [ ] = [ 1 , 2 , 3 ]
let list2: Array < number > = [ 1 , 2 , 3 ]
元组
元组类型允许表示一个已知元素数量和类型的数组,各元素类型不必相同
let x: [ string , number ] ;
x = [ 'hello' , 10 ]
x = [ 10 , 'hello' ]
枚举
enum 类型是对JavaScript标准数据的一个补充,使用枚举类型可以成为一组数值赋予友好的名字.
enum Color{ Red, Green, Blue}
let c: Color = Color. Green
unknown
有时候,我们想为那些在编程阶段还不清楚类型的变量指定一个类型,那么我们可以使用unknown类型类标记这些变量
let notype: unknown = 4 ;
notype = '张三'
notype = false
void
当一个函数没有返回值时,你通常会见到其返回值类型是void
function test ( ) : void {
console . log ( '此函数没有返回值' )
}
null 和 undefined
在TypeScript 里,undefined 和 null 两者各有自己的类型 分别叫做undefined 和 null
let u: undefined = undefined
let u: null = null
联合类型
联合类型 表示取值可以为多种类型中的一种.
let abc: string | number ;
abc = 'zhansgan'
abc = 123
2.条件语句
条件语句用于基于不同条件来执行不同的动作,TypeScript 条件语句是通过一条或多条语句执行结果(true/false) 来决定执行的代码块
if 语句
let num: number = 5 ;
if ( num > 0 ) { console . log ( '数字是正数' ) }
if ... else 语句
let num1: number = 12 ;
if ( num1 % 2 == 0 ) {
console . log ( 'num1是偶数' )
} else {
console . log ( 'num1是奇数' )
}
if ... else if ... else 语句
switch ... case 条件判断语句
3.函数(方法)
函数是一组一起执行一个任务的语句,函数声明要告诉编译器函数的名称,返回类型和参数,TypeScript 可以创建有名字的函数和匿名函数,其创建方法如下
function add ( x: number , y: number ) : number {
return x + y
}
let x = function ( x: number , y: number ) : number {
return x + y
}
函数的可选参数
在TypeScript 里我们可以在参数名旁使用(?)实现可选参数的功能,比如:我们想让lastname 是可选的
function buildName ( firstname: string , lastname? : string ) : string {
if ( lastname) {
return firstname + lastname
} else {
return firstname
}
}
let result1: string = buildName ( '张三' )
let result2: string = buildName ( '张三' , '王五' )
函数的剩余参数
剩余参数会被当做个数不限的可选参数,可以一个都没有,同样也可以有任意个,可以使用省略号(...)进行定义
function shengyucanshu ( firstname: string , ... restname: string [ ] ) {
return firstname + restname. join ( '' )
}
箭头函数
js 的箭头函数 剩余参数匹配js 中的arguments
let ( x, y, . . arg) => { }
剩余参数是个数组,它即使没有值,也是以一个空数组形式存在。
ts :
( [ parms1, parms2, ... parms n] ) => { }
let a = ( [ parms1, parms2, ... parms n] ) => { }
4.类
typescript 支持基于类的面向对象的编程方式,定义类的关键字为class,后面紧跟类名,类描述了所创建的对象共同的属性和方法
创建类
class Person {
private name: string
private age: number
constuctor ( name: string , age: number ) {
this . name = name
this . age = age
}
public getPersonInfo ( ) : string {
return ` 姓名 ${ this . name} ,年龄 ${ this . age} `
}
}
实例化类
let zhangsan = new Person ( '张三' , 18 )
zhangsan. getPersonInfo ( )
继承
继承就是子类继承父类的特征行为,使子类具有父类相同的行为,typescript 中允许使用继承来扩展现有的类,对应的关键字为extends
class Son extens Person {
private department: string
constructor ( name: string , age: number , department: string ) {
super ( name, age)
this . department = department
}
public getDepartment ( ) : string {
return this . getPersonInfo ( ) + ` 工作地点: ${ this . department} `
}
}
let lisi = new Son ( '李四' , 20 , 'jd' )
lisi. getPersonInfo ( )
lisi. getDepartment ( )
5.模块
模块 即js 中的module 模块可以相互加载,并可以使用特殊指令 export 和 import 来交换功能 , 从另一个模块调用一个模块的函数
两个模块之间的关系是通过在文件级别上使用 import 和 export 建立的 , 模块里面的变量, 函数和类等在模块外部是不可见的,除非明确的使用 export 导出他们,
export class NewsData {
title: string
content: string
constructor ( title: string , content: string ) {
this . title = title
this . content = content
}
}
import { NewsData } from '../?/NewsData.js'
6.迭代器 (循环)
当一个对象实现了Symbol.iterator 属性时,我们都是认为他可以迭代的 可遍历的 一些如内置的 Array , Map Set String int32Array 等都是具有可迭代性
for ... of 语句
let someArray = [ 1 , 'string' , false ]
for ( let item of someArray ) {
console . log ( item)
}
for ... in / for . . of
let list = [ 4 , 5 , 6 ]
for ( let i in list) {
console . log ( i)
}
for ( let item of list) {
console . log ( item )
}