ts 学习笔记(二)变量声明和接口

// ? var let const js语法规则
/*
var:
1、没有作用域块,声明在全局作用域是全局变量,声明在函数内部就是局部变量
2、有变量提升
let:
1、有作用域块
2、没有变量提升
3、已经声明的变量 不可再次声明
const:
1、有作用域块
2、没有变量提升
3、一旦声明切不可改变
4、已经声明的变量 不可再次声明
*/

// { 
//   var num1:number = 10 
// }

// console.log('num1 :>> ', num1); //10 

// {
//   let num2:number = 20
// }
// console.log('num2 :>> ', num2); //报错  找不到num2 存在作用域块

/************************************************************************** */

// console.log('num3 :>> ', num3);  报错 找不到num3 没有变量提升
// let num3:number = 10

// console.log('num4 :>> ', num4);
// var num4:number = 20

/************************************************************************** */

// let c = 1
// let c = 2

//! 接口 对定义好的结构,进行类型检查

// 在使用接口定义对象的时候,以声明的属性不能少,会报错
// 如果需要可选属性,可在该属性后加?
// 只读属性,在属性前 加 readonly;一但初始化,不可更改

//? 注意 做为变量使用的话用 const,若做为属性则使用readonly
// interface labelValue {
//     label: string
//     value: number
//     test?: any
// }

// function fn (o:labelValue) {
//     console.log('label :>> ', o.label);
// }


// let myObj:labelValue = {
//     label: 'hello',
//     value: 1
// }

// fn(myObj)

/*********************************************************************************** */

// interface SquareConfig {
//     color?: string;
//     width?: number;
// }

// function createSquare(config: SquareConfig) {
//     // ...
// }

// let mySquare = createSquare({ colour: "red", width: 100 }); //代码直接报错,自动纠错  很不错的功能

/*********************************************************************************** */

// interface SquareConfig {
//     color?: string;
//     width?: number;
//     [propName: string]: any;
// }

// const o:SquareConfig = {
//    length:1
// }

//? 函数类型

// interface testType {
//     (a:number,b:string):number
// }

// let test:testType = function (a,b){
//    return a+Number(b)
// }

//? 可索引类型
interface strArray {
    [index:number]:string
}

let arr1:strArray = ['a', '1']
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值