ts detailed explanation

Introduction to ts

The full name of ts is TypeScript
TypeScript can be understood as an extension of JavaScript, which extends the syntax of js, so we can continue to write js code in ts without reporting an error

TypeScript is also called static JavaScript. It cannot be directly introduced into HTML, and cannot be directly recognized by browsers. It needs to be converted by ts converter or babel before it can be used.

How to understand static language and dynamic language

The type system is classified according to the timing of type checking, and is divided into dynamic type and static type. Dynamic is to check the data type at runtime, and static is to check the data type at compile time.

1 Static language will check the data type during compilation, that is, declare the data type of the variable when writing the code. Most of the background languages ​​such as java, php, etc. and the ts we want to learn are static.
2 A dynamic language refers to a language that does type checking during runtime, that is to say, a dynamic language does not need to specify a data type when declaring it. For example, both javascript and python are dynamic.

Liezi Description

//单属性
let a:Number=18
let b:symbol=Symbol()
console.log(b,"asdasd");
let c:number=15

//单属性多类型
let timer:null|number=null
type s =string
const ss:s='abc'

//数组两种方式
let numArr:number[]=[1,2,3]
let strArr:Array<string>=['1','2','3']
//函数
function add(a:string,b:number):any{
    
    
    return a+b
}
//箭头函数
const fun=(a:number=1,b:number=1):any=>{
    
    
    return a+b
}
fun(1,1)
//函数简写
type Fn=(ni:number,n2:number)=>number
const fun3:Fn=(a,b)=>{
    
    
    return a+b
}
//可选参数
function fun8( a?:number,b?:number){
    
    
    console.log(111);  
}
function fun88( a:number,b:number){
    
    
    console.log(111);  
}
fun8()
fun8(1,2)
fun8(1,2)


// 对象   
// const 对象名: {
    
    
//     属性名1:类型1,
//     属性名2?:类型2,
//     方法名1(形参1: 类型1,形参2: 类型2): 返回值类型,
//     方法名2:(形参1: 类型1,形参2: 类型2) => 返回值类型
//   } = { 属性名1: 值1,属性名2:值2  }
//l类型别名Type

type obj={
    
    
    a:string,
    b?:number,
    fun10():void
}
let obj1:obj={
    
    
    a:'1',
    b:18,
    fun10(){
    
    }
}

//两种形式描述对象 1:类型别名 type   2:接口 interface

//interface声明接口
interface obj2{
    
    
    name:string,
    price:number,
    fun11:()=>string
}
//利用声明得接口名称作为变量的名称
const good1:obj2={
    
    
    name:'1',
    price:200,
    fun11:function(){
    
    
        return '1'
    }

}
const good2:obj2={
    
    
    name:'1',
    price:200,
    fun11:function(){
    
    
        return '1'
    }

}

//接口得继承
//注意:在日常开发中   如果两个接口之间又相同得属性或者方法,可以将(公共得属性或者方法抽离出来,通过继承extends实现复用)语法:
 interface obj3{
    
    
    a:number,
    b:number,
 }
//  interface obj4{
    
    
//      a:number,
//      b:number,
//      c:number
//  }

//obj4利用继承

interface obj4 extends obj3{
    
    
    c:number
}//这样obj4就用了obj3得属性或者方法(obj4用 a b c 三个属性)

Guess you like

Origin blog.csdn.net/weixin_48466991/article/details/129117595