C/C++教程

Day167/200 自动执行TypeScript和interface、type 区别

本文主要是介绍Day167/200 自动执行TypeScript和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

这篇关于Day167/200 自动执行TypeScript和interface、type 区别的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!