美文网首页
ts基本用法

ts基本用法

作者: 0说 | 来源:发表于2024-02-13 14:48 被阅读0次
    type Atype = (string | number)[]
    let arr: Atype = ['666', '666']
    console.log(arr)
    
    function add (num = 2, num1: number): number {
      return num + num1
    }
    add(1, 2)
    
    const arr2 = (num = 2, num1: number): number => {
      return num + num1
    }
    arr2(1, 3)
    // 函数式声明
    const arr3: (num: number, num1: number) => number = (num = 333, num1) => {
      return num + num1
    }
    
    arr3(1, 3)
    
    function nameFn (name: string):void {
      console.log(name)
    }
    nameFn('666')
    // 参数可传可不传 参数 后面加一个 ? 
    function nameFn2 (name?: string):void {
      console.log(name)
    }
    nameFn2()
    
    // 对象类型 
    let person: {name: string; age: number; say(name: string): void} = {
      name: 'jame',
      age: 6,
      say: (name) => {
        console.log(name)
      }
    }
    person.say('james')
    
    // 对象类型  如果多选可以把 ; 去掉
    let person2: {
      name: string
      age: number
      say(name: string): void
      say2: (name: string) => void
    } = {
      name: 'jame',
      age: 6,
      say: (name) => {
        console.log(name)
      },
      say2: (name) => {
        console.log(name)
      }
    }
    person2.say2('james2')
    
    // 可选参数  methods 可有值也可以无值
    const myAxios = (config: {url: string, method?: string}): void => {
      console.log(config)
    }
    myAxios({
      url: 'aaaa'
    })
    myAxios({
      url: 'aaaa',
      method: 'postw'
    })
    
    
    // 接口 interface
    interface Myparson {
      name: string
      age: number
      say: (name: string) => string
    }
    const parson3: Myparson = {
      name: 'jame',
      age: 6,
      say(name) {
        return name
      }
    }
    parson3.say('6666')
    
    // 继承 extends
    interface myExtends {
      label: string
      value: string
    }
    interface myExtends2 extends myExtends {
      id: number
    }
    const opt: myExtends2 = {
      label: 'aaaa',
      value: '555',
      id: 5555
    }
    console.log(opt)
    
    // 元组: 元组类型是另一种类型的数组,它确切地知道包含多少个元素,以及特定索引对应的类型
    // 例如
    const post:number[] = [12, 114, 555] // 可无限加
    const postPoint:[number, number] = [12, 114] // 只允许2个
    console.log(post, postPoint)
    
    // 字面量类型 直接用特定的值做为类型
    let numT: 18 = 18
    // numT = 19 // 这里就会报错 因为 numT类型为18 不能将类型“19”分配给类型“18”
    console.log(numT)
    // 用处 固定要几个值 不能其他值
    const changePost = (post: 'top' | 'left' | 'bottom' | 'right') => {
      console.log(post)
    }
    // changePost('666') // 这里参数不能传其他的 只能上面几个值    类型“"666"”的参数不能赋给类型“"top" | "left" | "bottom" | "right"”的参数
    changePost('top')
    
    // 枚举 enum 定义出几个值  注意:形参 post 的类型为枚举 PostType,那么,实参的值就应该是枚举 PostType 成员的任意一个访问枚举成员
    enum PostType {
      Up='up', // 赋值给Up 如果没有赋值时 0 1 2 3序号值
      Left='left',
      Right='right',
      Bootom='bottom'
    }
    
    const changePost2 = (post: PostType) => {
      console.log(post)
    }
    // 类似于JS 中的对象,直接通过点(.)语法访问枚举的成员。
    changePost2(PostType.Up)
    
    enum PostType2 {
      Up=14, // 赋值14 下面序号值会自增长
      Left,
      Right,
      Bootom
    }
    console.log(PostType2.Left) // 15
    
    // typeof 类型推论 
    const pArg = {x: 6, y: 10}
    //                  这里会推断出 PA的类型
    const pArgFn = (pA: typeof pArg) => {
      console.log(pA)
    }
    pArgFn(pArg)
    // 也可以查询对象里的某个属性值的类型
    let _pArg: typeof pArg.x = 666
    console.log(_pArg)
    

    相关文章

      网友评论

          本文标题:ts基本用法

          本文链接:https://www.haomeiwen.com/subject/lnajadtx.html