TypeScript能够编译出纯净、 简洁的JavaScript代码,而且能够运行在任何浏览器上、Node.js环境中和任何支持ECMAScript 3(或更高版本)的JavaScript引擎中。下面咱们将从最基础的了解typescript。react
const foo = (x:string,y:number) => { console.log(x) } foo("s",2)
//编译成es5 var foo = function (x, y) { console.log(x); }; foo("s", 2);
二、接口typescript
interface Foo { name: string; age: number; }//接口类型的定义 const child = (x:Foo) => { console.log(x.name+","+x.age) } child({ name: "hou", age: 12 }) //编译成es5 var child = function (x) { console.log(x.name + "," + x.age); }; child({ name: "hou", age: 12 });
三、类浏览器
interface Foo { name: string; age: number; } class Fzz implements Foo { //实现接口 name: string; age: number; constructor(n:string) { this.name = n } sayName() { return this.name } } const fzz = new Fzz("hou") //实例化类,react自动给你实例化了 fzz.sayName() //编译成es5 var Fzz = /** @class */ (function () { function Fzz(n) { this.name = n; } Fzz.prototype.sayName = function () { return this.name; }; return Fzz; }()); var fzz = new Fzz("hou"); fzz.sayName();