Home > Article > Web Front-end > How to go from JavaScript to TypeScript?
The content of this article is about how to go from JavaScript to TypeScript? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
If you want to learn typeScript, I think you must first be very proficient in native javaScript. The most basic knowledge is the most important, and then you must master ES5 ES6 ES7 (it is best to know some after 7). Once you master the new technology, you can It won't be so tiring. typeScript =type javaScript, adds a type on the basis of ES5/6/7 javaScript!
TS is really very strict compared to JS, as long as it slightly does not match the interface or value type, or it is If the number of parameters is incorrect, or if the value is changed and the type of the original value is not
consistent, an error will be reported. It is recommended to use npm to install typeScript globally and then use tsc *.ts to compile TS files
'The new core concept of typeScript:'let app:string=2;This code will report an error because the value 2 is a number, and the app is specified as string type, so an error will be reported in TS
New value type:
any: can be any typeclass ask{ name:string; tel ? :number ; //这里为什么加问号,因为你不一定能拿到她的号码,如果拿不到,那么便可以不传参数, 但是如果不加? ,你又没那么号码,那么你没有参数传进来,TS就会报错 age:number ; constructor(name,age,tel){ this.name=name; this.age=age; this.tel=tel } } interface check { name : string; age:number; tel ? :number; } let woman :check = new ask ('rose',20,1888888888);//假设你拿到美女所有资料 传入构造函数This way You can print console.log(woman) to see what it looks like. There is no error in the above code.
var ask = /* @class / (function () { function ask(name, age, tel) { this.name = name; this.age = age; this.tel = tel; } return ask; }()); let woman = new ask('rose', 20, 1888888888); console.log(woman)"It should be noted here that after many TS codes are compiled, they are not Will appear in JS files, such as const enum enumerations, interfaces, etc. Of course, you will encounter more "
concepts of tuples
const enum Directions { Up, Down, Left, Right } let directions = [Directions.Up, Directions.Down, Directions.Left, Directions.Right]; // 结果是 0 1 2 3
* The difference between a constant enumeration and a normal enumeration is that it will be deleted during the compilation phase and cannot contain calculated members. You will know after a try
Classes and interfaces (one of the most important)
* If you want to implement Multiple classes inherit, then use subclasses to continue to inherit other classes, and the cycle continues
interface check { name: string; age: number; hobby: Array<number> fuck: number[] //这两种写法是一样的 }
class exp implements check { name: string age: number hobby:Array<number> fuck: number[] constructor(name, age, hobby, fuck) { this.name = name; this.age = age; this.hobby = hobby; this.fuck = fuck; } } let app = new exp('hello', 18, [1, 2, 3], [2, 3, 4])------After TS compilation, you get
var exp = /* @class / (function () { function exp(name, age, hobby, fuck) { this.name = name; this.age = age; this.hobby = hobby; this.fuck = fuck; } return exp; }()); var app = new exp('hello', 18, [1, 2, 3], [2, 3, 4]);
This article has ended here. For more exciting content, you can pay attention to the JavaScript Tutorial Video column on the PHP Chinese website!
The above is the detailed content of How to go from JavaScript to TypeScript?. For more information, please follow other related articles on the PHP Chinese website!