TS内容介绍

 ts介绍

 ts全称为typeScript 是type+Script为JavaScript的超类

typeScript定义变量时要声明变量的类型 以及赋给变量正确的数据类型的值

const hello : string = 'Hello World!'

const world : number = 'Hello World!'

console.log(hello , typeof hello)

ts基础数据类型

/*

 *   任意数据类型  any  声明为 any 的变量可以赋予任意类型的值

 *   数字类型      number  

 *   字符串类型    string

 *   布尔类型      blooean

 *   数组类型      在元素类型后面加上[]

                  let arr: number[] = [1, 2];

                  // 或者使用数组泛型

                  let arr: Array<number> = [1, 2];

 *   元组          元组类型用来表示已知元素数量和类型的数组,各元素的类型不必相同,对应位置的类型需要相同

                  let x: [string, number];

                      x = ['Runoob', 1];    // 运行正常

                      x = [1, 'Runoob'];    // 报错

                      console.log(x[0]);    // 输出 Runoob

 *   枚举          enum  枚举类型用于定义数值集合

                  enum Color {Red, Green, Blue};

                  let c: Color = Color.Blue;

                  console.log(c);    // 输出 2

 *   void   void  用于标识方法返回值的类型,表示该方法没有返回值。

                  function hello(): void {

                  alert("Hello Runoob");

                  }

 *  null    null  表示对象值缺失

 *  undefined  undefined  用于初始化变量为一个未定义的值

 *  never   never  never是其他类型(包括null和undefined)的子类型,代表从不会出现的值

 */


 

// any类型

// 1、变量的值会动态改变时,比如来自用户的输入,任意值类型可以让这些变量跳过编译阶段的类型检查

let x : any = 1  //数字类型

x = 'I am who I am'  //字符串类型

x = false  //布尔类型


 

// 枚举 以及枚举的含义

enum Color {red, green, blue}

let c : Color = Color.blue

console.log(c)

// 例一

enum Direction {

    Up, // 0                                                                                                                                                                      

    Down,// 1

    Left,// 2

    Right// 3

}

console.log(Direction.Up,Direction.Down,Direction.Left,Direction.Right)

// 例二

// enum Direction {

//     Up = 1,// 1

//     Down,// 2

//     Left,// 3

//     Right// 4

// }

// 例三

// enum Direction {

//     Up,// 0

//     Down=3,// 3

//     Left,// 4

//     Right// 5

// }

// 例四

enum Days {

    Sun = 3,

    Mon = 1,

    Tue,

    Wed,

    Thu,

    Fri,

    Sat

}

console.log(Days.Sun,Days.Mon,Days.Tue,Days.Wed)

console.log(Days["Sun"] === 3)

console.log(Days["Wed"] === 3)

console.log(Days[3] === "Sun")

console.log(Days[3] === "Wed")

/*

 * 数字枚举如果没有初始化,默认初始化值为0,每项+1

 * 如果有初始化,则在初始化值的基础上,每项+1

 * 如果某项被赋值(可以是负数或负数),那么之后的项在此基础上+1

 *如果未手动赋值的枚举项与手动赋值的重复了,如例4,TypeScript 是不会察觉到这一点的,但建议尽量避免

 */

// type 定义数据类型 type使用场景 给复杂类型起别名

type num = number

const number : num = 12321

const number1 : num = 324324

console.log(number, typeof number, number1, typeof number1)

// interface 表示为接口

interface Shape {

    name : string

    width : number

    height : number

    color ?: string // ?: 为可选属性(可选参数,可以理解为参数自动加上undefined)

}

function area(shape : Shape) {

    let area = shape.width * shape.height

    return " I'm " + shape.name + " with area " + area + " cm squared "

}

console.log(area({name: 'rectangle', width: 30, height: 15}))

console.log(area({name: 'square', width: 30, height: 30, color: "red"}))

interface FooterballPlayer {

    name : string,

    age ?: number,

    talent ?: string,

    hadGolden ?: boolean

}

function player(player : FooterballPlayer) {

    return player.name + player.age + player.talent + player.hadGolden

}

console.log(player({name:'messi', age:35, talent:'全能前锋', hadGolden:true}))


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值