TypeScript基础

11 篇文章 0 订阅
1 篇文章 0 订阅

【 一 】传递参数的个数:多传或者少传报错

优点方便后续更新迭代少传或者多传报错

function buildName(fristName: string, lastName: string, ageName: string) {
return fristName + ‘’ + lastName + ageName;
}
let result1 = buildName(‘bob’, ‘pop’, ‘aa’);
console.log(result1);

未传递的参数的时候可以设置默认值:

function initValue(fristName: string, lastName: string = “hanbi”) {
return fristName + “” + lastName;
}
let result = initValue(“zhan”, “000”);
console.log(result);

当传入的参数不确定的时候:可以使用扩展运算符来解决

function buildName(fristName: string, …lastName: string[]) {
return fristName + ‘’ + lastName.join(’’);
}
let result = buildName(‘frist’ + ‘lst’ + 'tse ');
console.log(result);

【 二 】Tsx全局挂载变量

(window as any).test=111
console.log(window)

【 三 】map对象

let nameSiteMapping = new Map();
nameSiteMapping.set(‘google’, 1);
判断是否有该map对象
console.log(nameSiteMapping.has(‘google’));
删除map对象
console.log(nameSiteMapping.delete(‘google’));
清除所有的键值对
console.log(nameSiteMapping.clear());
console.log(nameSiteMapping.get(‘google’));

【 四 】元组

我们一般数组里面要不是纯的数字要不就是字符串,
let mytuple = [10, ‘元组’];
console.log(mytuple[0]);
console.log(mytuple[1]);

【 五 】联合类型进行使用

function disp(name: string | string[]) {
if (typeof name == ‘string’) {
console.log(name);
} else {
for (let i = 0; i < name.length; i++) {
console.log(name[i]);
}

  console.log(...name);
}

}
disp(‘roab’);
disp([‘aa’, ‘bb’, ‘cc’, ‘dd’]);

【 五 】接口检测类型和参数

interface Iperson {
fristName: string;
lastName: string;
age: number;
sayHi: () => string;
}
const customer: Iperson = {
fristName: ‘张三’,
lastName: ‘李四’,
age: 10,
sayHi: (): string => {
return ‘hi 我是方法’;
},
};
console.log(customer.sayHi);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值