“This is the 13th day of my participation in the August Text Challenge.
TYPESCRIPT
- website
- github
TypeScript is an open source, cross-platform programming language developed by Microsoft. It is a superset of JavaScript that will eventually be compiled into JavaScript code.
See TypeScript Tooling in 5 minutes
Install the TypeScript
Run the following command from the command line to install TypeScript globally:
npm install -g typescript
Copy the code
After the installation, run the following command on the console to check whether the installation is successful:
tsc -V
Copy the code
The first TS program
Create a new file test1.js
const str:string ="hello world TS";
console.log(str)
Copy the code
The TSC command will generate js files in the same layer
tsc test1.ts
Copy the code
Perform the node test1. Js
PS E:\mysjc\ts> node test1.js
hello world TS
PS E:\mysjc\ts>
Copy the code
Congratulations you have already started ts, back to look at ts and JS what is the difference, careful you have found ts more than a type, then someone will ask what is the type? What are the types?
- A type is a set of keywords that declare the basic data type or abstract data structure type of an identifier
- The type determines what kind of data you want to store in memory, right
The base type
type
string
Use single ‘or double’ quotation marks to indicate string types. Backquotes’ to define multi-line text and inline expressions
const str1: string = "hello world"; const str2: string = 'ts'; Const str3: string = '${str1} ${str2}';Copy the code
number
const num1: number = 3; // decimal const num2: number = 3.3; Const num3: number = 3.333; const num4: number = 0b11; // binary const num5: number = 0o12; // octal const num6: number = 0xa; // HexadecimalCopy the code
boolean
Logical values: true and false
const bol1: boolean=false;
const bol2: boolean=true;
Copy the code
An array of
- The element type is followed by []
- Array< element type >
Const list1: number [] = [1, 2, 3]. // Try list1[0]='2' Const list2: Array < number > = [1, 2, 3].Copy the code
The enumeration
enums
enum Animal { Dog, Cat, Pig } let zhangsan: Animal = Animal.Dog console.log(zhangsan, Animal.Cat, Animal.Pig) ; / / 0 1 2Copy the code
So 0,1,2, I want John to be a dog, and I can assign it directly, and now John is a real dog.
Enum Animal {Dog=" Dog ", Cat=" Cat ", Pig=" Pig "} let zhangsan: Animal = Animal.Dog console.log(zhangsan, Animal.Cat, Animal.Pig) ; / / dogs and cats pigsCopy the code
any
It can be any type
let a: any = [1,undefined,null,'string',true,false];
a[0]='s';
console.log(a); //[ 's', undefined, null, 'string', true, false ]
let b: any=1;
b='wc';
b=null;
console.log(b); //null
Copy the code
object
The argument is of type Object
// The parameter's type annotation is an object type
function printCoord(pt: { x: number; y: number }) {
console.log("The coordinate's x value is " + pt.x);
console.log("The coordinate's y value is " + pt.y);
}
printCoord({ x: 3, y: 7 });
Copy the code
The object type can also specify whether a property is optional
function printName(obj: { first: string; last? : string }) { console.log(`${obj.first}-${obj.last}`); } printName({ first: "Bob" }); //Bob-undefined printName({ first: "Alice", last: "Alisson" }); //Alice-AlissonCopy the code
If the optional argument is not passed, the obtained value is undefined, which can be checked before use.
function printName(obj: { first: string; last? : string }) { // obj.last.toUpperCase(); // TypeError: Cannot read property 'toUpperCase' of undefined if (obj.last ! == undefined) { console.log("if", obj.last.toUpperCase()); } console.log(obj.last? .toUpperCase()); } printName({ first: "Bob" }); // undefined /** * if AA * AA */ printName({ first: "Bob", last: "aa" });Copy the code
Union Union type
A union type is a type made up of two or more other types
/ * * * * @ param id type can only transfer number or string * / function work (id: number | string) {the console. The log (" work: "+ id); } work(101); // Work :101 work("202"); // work :202 // work(false);Copy the code
If each member of the union is valid, you can do something interesting
/ * * * @ param id type can only transfer number or string * / function work (id: number | string) {const salary: number = 300; If (typeof id = = = "string") {the console. The log (` ${id} to work, can get paid $${salary} `); } else {the console. The log (` ${id} go to work, can get paid $${2} salary * `); } } work(101); // work("202"); // if you work, you will be paid $300Copy the code
Type Assertions Type assertion
Type assertion is like conversion in other languages, but without special data checking and deconstruction. It has no run-time impact, only at compile time
- < type > value
- Value as type
console.log(1 as string)
console.log(<string>1 )
Copy the code
Type inference
TS will infer a type when no type is explicitly specified
- The variable was assigned when it was defined, inferred to the corresponding type
- The variable is defined without an assignment and is inferred to be of type any
let a = 1; a = '11'; // Can't assign type "string" to type "number" let b; b = 1; b = '1'; b = null;Copy the code