TypeScript - (三) TypeScript中变量的声明及使用

附上仓库:  https://github.com/chenrui123456/TypeScript-Demo

TypeScript中类型一共有: 

1.boolean 布尔
2.string 字符串
3.number 数值 
4.array or [] 数组
5.tuple 元组
6.enum 枚举
7.any 任何类型
8.void 
9.null 
10.undefined

1. boolean类型. 值分为true和false.

let flag: boolean = true;
console.log('flag:', flag);

2.string 类型.

let name: string = 'cr';
console.log('name:', name);

3. number类型,可包含小数.

let age: number = 22;
console.log('age:', age);

4.string的额外用法: ` 是键盘上ESC下面的反引号 而不是单引号.

let userInfo: string = `age:${age} , name: ${name}`;
console.log('userInfo: ', userInfo);

5. 数组类型  [] 或者 Array. 需要使用泛型约束.

let numberList: number[] = [1,2,3];
let numberArray: Array<number> = [1,2,3];
console.log('numberList:', numberList);
console.log('numberArray:', numberArray);

6. 元组(不是泛型的不可变长度的数组), 已知数量和类型的数组,各元素类型可以不同.

let tupleList: [number,string] = [age,name];
console.log('tupleList:', tupleList);

7. 枚举(有点像map),如果枚举Key的值是number的话,将会从0开始自增。 如果第一个是1的话,其余会自增.

// key = value
enum Color {
    red = 1,
    green = 2,
    blue = 5
}
// 根据key找value, 使用枚举名称.Key名称可以找到对应的value
let colorValue = Color.red;
console.log('colorValue:', colorValue);
// 根据value找key, 使用枚举名称[对应的值]可以找到对应的key
let colorKey: string = Color[5];
console.log('colorKey: ', colorKey);

8. any , 任何类型. 如果有些变量不想使用ts的强制类型约束就可以使用any进行声明。

let notSureType: any = 'notSureType';
console.log('notSureType:', notSureType);
notSureType = true;
console.log('notSureType:', notSureType);
notSureType = [1,2,5];
console.log('notSureType:', notSureType);
/**
 * print info
 * notSureType: notSureType
 * notSureType: true
 * notSureType: [ 1, 2, 5 ]
 * */

9. void , null , undefined.

/**
 *  void类型的变量声明下来只能赋值null或undefined,所以不会使用void类型
 *  null和undefined是所有类型的子类型,其余类型在声明赋值时可以直接使用null或undefined使用,
 *  当然我不建议这样做。
 * */

附上图片:

1. 字符串使用占位符的用法要确保变量是存在的.

2.元组的使用要确保元素类型和数量一致.

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值