TypeScript知识

143 阅读3分钟

变量声明

var/let/cost 标识符:数据类型(类型注解)=赋值;不推荐使用var关键字

let age:number=123 

数据类型有大小写之分有区别 ts代码中要用小写

let msg:string="hellow" //string:typeScript中的字符串类型
let msg:String="hellow" //String:javaScript的字符串包装类的类型

类型推导 默认变量赋值时候,类型注解会添加值的类型:

let age=123
等于
let age:number=123

数据类型

1.基础类型

number

let num:number=100 //十进制
let num:number=0b110 //二进制
let num:number=0o555 //八进制
let num:number=0xf23 //十六进制

boolean

true/false

null

const n1:null=null

undefined

const n1:undefined=undefined

symbol

//同样的key不同的值
const title1=Symbol("title")
const title2=Symbol("title")
const info={
    [title1]:"程序员",
    [title2]:"老师"
}

bigInt

const max = Number.MAX_SAFE_INTEGER; 
const max1 = max + 1 
const max2 = max + 2 max1 === max2 // true
可以看到,最终返回了true,这就是超过精读范围造成的问题,而`BigInt`正是解决这类问题而生的:
const max = BigInt(Number.MAX_SAFE_INTEGER); 
const max1 = max + 1n 
const max2 = max + 2n max1 === max2 // false
2.复杂基础类型

any

//可以赋值任意类型
 let message:any="hellow"
 message=123
 message={}

array

//数组中的数据类型最好统一
const names01:Array<string>=[] //不推荐(react jsx中有冲突)
const names01:string[]=[] //推荐

object

const info={
    name:"skyline",
    age:18
}

unknown

//unknown类型只能赋值给any和unknown类型
//any类型可以赋值给任意类型
let msg01:any=111;
let msg02:unknown=111;
let result:string;
result=msg01;
result=msg02;//报错

void

//函数如果没有返回值就是void类型
//js函数中不写返回值默认 return undfined

//定义函数类型
cons foo:()=>void=()=>{
}

never

//一个函数没有永远不返回结果或者抛出异常 定义为never类型

tuple

//元祖类型 确定元素中元素的每个类型
//多种元素的组合
const info:[string,number,number]=["why",18,1.88]

类型相关知识

函数

function fn(this:{namd:string},payload:{x:number,y:number,z?:number}):number{
}

//函数重载
function add(num1:number,num2:number):number;
function add(num1:string,num2:string):number;
function add(num1:any,num2:any):any{
    if(typeof num1==="string"&&typeof num2==="string"){
        return num1.length+num2.length;
    }
    return num1+num2;
}

联合类型

function fn(id:number|string):number{
}

非空类型断言

function fn(id?:number|string):number{
    let temp=id!;
}

可选链

function fn(id?:number|string):number{
    let temp=id?;
}

js运算符!!

const message ="hellow";
const flag=!!message;//转布尔

js ??空值合并操作符

let message:string|null=null/undefined
const content=message??"你好啊"
//等于
const content=message?message:"你好啊"

字面量类型

const message:"hellow"|"hi"="hellow" //必须都是hellow/hi

字面量推理

type Method="GET"|"POST"
function request(url:string,method:Method){}
const options={
    url:"http",
    method:"POST" //method的值可以随意改变不安全所以下边的options.method会报错
}
requesst(option.url,option.method as Method);

类型缩小

//typeof 判断类型
//平等缩小(比如 ===  !==) 判断值
//instanceof 判断对象是否属于某个类实例
//in 对象有没有某个方法
type Fish={swimming:()=>void}
type dog={running:()=>void}
function walk(animal:Fish|dog){
    if("swimming" in animal){
        animal.swimming();
    }else{
        animal.running();
    }
}
const fish=Fish=>{
    swimming(){
    }
}

//格式
class parson{   
    name:string:"parson"
    constructor(name?:string){
        this.name=name;
    }
    eatting(){
        console.log("eating");
    }
}
//继承
class student extents parse{
}

//修饰符
public 默认 
private私有 
protected在类内部和子类可以访问外部不行
readonly只读 能在constructor中赋值,赋值之后不能修改

接口

//定义可选参数
interface infotype{
    name:string,
    age?:number
}
//定义只读属性
interface infotype{
    readonly name:string,
    age:number
}
//定索引类型
interface infotype{
    [index:number]:string,
}
//定义函数类型
interface infotype{
    (num1:number,num2:number):number
}
//接口继承
interface ISwim{
    swiming:()=>void
}
interface IFly{
    flying:()=>void
}
inter IAction extents ISwim,IFly{
}
//交叉类型
interface ISwim{
    swiming:()=>void
}
interface IFly{
    flying:()=>void
}
type IType ISwim & IFly;//必须有 swiming和flying

泛型(类型参数化)

//函数
function fn<Type>(a:Type,b:Type):Type{
}
fn<number>(1,2);
//类
class Point<T>{
    x:T
    y:T
    z:T
    constructor(x:T,y:T,z:T){
        this.x=x;
        this.y=y;
        this.z=Z;
    }
}
const p1=new Point("1.33.2","1.33.2","1.33.2");
const p2=new Point<string>("1.33.2","1.33.2","1.33.2");
const p2:Point<string>=new Point("1.33.2","1.33.2","1.33.2");

//常用
interface Ilength{
    length:bumber
}
function getLength<T extends Ilength>(arg:T){
    return arg.length;
}
getLength('abc');
getLength(['a','b']);
getLength({length:9});

其他补充