typescript 语法总结

export {}

let name: number = 10
console.log(name)
let person: Array<number> = [10]
console.log(person)
let age:[number, string] = [10, '99']
// 枚举
enum Week  {
  a = 1,
  b = 2
}
const enum Colors {
  red, 
  yellow,
  blue
}
// !null undefined
let root: HTMLElement | null = document.getElementById('app');
root!.style.color = 'red'

// void 没有返回值 空的没有
function test(params:string): void {
  let actions = 1
  // return actions
}

// nerver 不会出现的值
// 会抛出错误导致无法结束
function crateNever(params:string): never {
  throw new Error("34343");
  
}
function sum(params:string): never {
  if (true) {
    console.log('a')
  }
  console.log('end')
  throw new Error("34343");


}
// 包装对象 
// 自动在基本类型对象上进行切换
// 基本数据类型上没有方法
// 2 会在内部做一个装箱操作,包装为对象类型,然后用对象来调用方法

let name2:string = '23423'
// let name22 = new String(name2)
// name22.toLocaleLowerCase()
name2.toLocaleLowerCase()


// 联合类型
let name3: string | number;
name3 = '23423'
name3.toLocaleLowerCase()
name3.toFixed()
 

// 类型断言 as 可以转为想要的类型
let name5 : string | number // = 3
(name5 as string).toLocaleLowerCase();
(name5 as number).toFixed();

// 字面量类型 
let gender: 'a' | 'b';
// 不可以再赋值了
// gender = 'c'
gender = 'a'


// 函数类型
type getuserType = (params: string, oldname: string) => {name: string}
let getUser: getuserType = function testname(params:string, oldname: string): {
  'name': string
} {
  return {name: '234324'}
}

// 函数的可选参数 默认参数
function print(name:string, age?: number, sex:string ='women') {
  
}
print('324', 2343)

// 剩余参数
function sums(...number:Array<number>) {
  return number.reduce((acc, item) => acc+item, 0)
}
sums(3,42,3)

// 函数重载 限定调用的格式
let aaa = {}
// 这两个是用来限定的
function attr(params: number): void;
function attr(params: string): void;
//
function attr(params: any): void {
  if (typeof params == 'string') {
    
  } else if (typeof params == "number") {
    
  }
}
attr('2343')
attr(2399324)
// attr(true) //不可以

// ts怎么写箭头函数 
// 和js是一样的
// 只是在定义类型的时候不一样
type delayType = (ms: number) => void
let delay:delayType = (ms: number) => {

}

//
namespace a{

  class Person {
    myname: string;
    age: number;
    constructor(name: string) {
      // 初始化
      this.myname = name
    }
    get name() {
      return this.myname
    }
    set name(newVal: string) {
      this.myname = newVal.toLocaleLowerCase()
    }
  }
  let a1 = new Person('lifengsan')
  a1.name = 'shishanxiang'
}


// 继承

namespace b {
  class Person {

    name: string;
    age: number;
    public color: string;
    private width: string;
    protected height: string;
    constructor(name: string, age: number) {
      this.name = name;
      this.age = age
    }
    getName() {
      return this.name
    }
    setName(newName: string) {
      this.name = newName
    }
  }
  class Student extends Person {

    // 类属性
    static Type = 'student'
    // sex: string
    constructor(public sex: string, age: number, name: string) {
      super(name, age)
      this.sex = sex
    }
    // 类属性
    static getType = 'hah'
    // 实例属性 在原型上
    getStu() {
      return this.sex
    }
    setStu(newSex: string) {
      this.sex = newSex
    }
  }
  let s = new Student('nan', 10, 'fengliha')
  // Student.Type
  // Student.getType()
}

// 访问修饰符 public private protected
// static 静态属性 静态方法

 

posted @ 2021-06-11 14:33  Mrco7837  阅读(54)  评论(0编辑  收藏  举报