标签:200 TypeScript console pt ts number 类型 interface type
前言
1、适合新手入门;
2、眼过千遍,不如手过一遍,看文档,我忘记了很多次;建议大家边看边运行Demo。
3、官网在线运行地址:https://www.typescriptlang.org/play
1、如何自动执行ts文件?
使用ts-node直接运行TypeScript代码
npm install -g ts-node
ts-node demo1.ts
2、函数的类型
即函数的返回值类型
function sanitizeInput(str: string): string {
return str + ',arden';
}
console.log(sanitizeInput('hi'));
3、Interface和type的共同点和区别
1)共同点
都可以声明类型
interface Point {
x: number;
y: number;
}
//type Point {
// x: number;
// y: number;
//}
function printCoord(pt: Point) {
console.log("The coordinate's x value is " + pt.x);
console.log("The coordinate's y value is " + pt.y);
}
printCoord({ x: 100, y: 200 });
2)不同点
类型 | 拓展方式 | 表列 B |
---|---|---|
interface | 通过extends 父类型拓展 | 可以向现有类型添加新字段 |
type | 通过 父类型 &{} 拓展 | 不可以向现有界面添加新字段 |
参考链接
https://www.typescriptlang.org
标签:200,TypeScript,console,pt,ts,number,类型,interface,type 来源: https://blog.csdn.net/xinghuowuzhao/article/details/121482781
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。