ts -- 1.数据类型

2020-11-16  本文已影响0人  Json766
let flag:boolean = true;
let arr1:number:[] = [1,2];
let arr2:Array<number> = [1,2];
let arr3:any[] = [1,'字符串',true];
let str:string = '字符串';
let num:number = 1;
let arr2:Array<number> = [1,2];
enum Flag{success = 1,error = -1};
let num1 = Flag.success;  //1
let num2 = Flag.error;  //-1

enum Fruit{apple,banana,pear = 5,orange};
let a = Fruit.apple;    //0
let b = Fruit.banana;    //1
let pear = Fruit.pear;    //5
let orange = Fruit.pear;  //6
let xBox =  document.getElementById('box');
let ev:number;  //错误,没有声名值
let ev:undefind;
let ev:null;
//没有任何返回值,一般用于定义方法的时候没有返回值
function fn():void{
  console.log('我没有返回值')
}
//从来不会出现的值(一般用于报错、出现异常)
let a:never;
  a=(()=>{
    throw new Error('这是一个错误')
 })()

扩展

const someValue:any = 'String';
const strLen = (someValue as string).length;
let tar:string | number;
tar = '1';
tar = 1;
type First = {first:number}
type Second = {second :number}
//扩展新的类型
type FirstAndSecond  = First & Second
function fn(params:FirstAndSecond):FirstAndSecond  {
  return {first:1,second:2}
}
上一篇下一篇

猜你喜欢

热点阅读