Introduction to TypeScript Basics 1

Install TypeScript

Use the command npm i typescript --save to install typescript for the current directory, but the tsc command of typescript cannot run typescript code directly, it just compiles ts code into js code and generates a js file, so usually we also install typescript The runtime of ts-node: npm i -g ts-node.
Note: ts-node is not equal to node.js of typescript, it only encapsulates the compilation process of typescript and provides the ability to run typescript directly.

Types and functions

  • basic type
Types of Example
boolean let flag:boolean = true
number let a:number = 6;let b:number = 1_000_000
string let name:string = “hello world”
null let u: null = null, null is a subtype of all types
undefined let u: undefined = undefined, undefined is a subtype of all types
Array let list: number[] = [1, 2, 3]或者let list: Array = [1, 2, 3]
Tuple let x:[string,number] = [“hello”,1] //x[0]=>‘hello’;x[1]=>1
any let notSure: any = 4;notSure = “maybe a string instead”;
enum enum Color {Red, Green, Blue}let c: Color = Color.Green;或者enum Color {Red = 1, Green = 2, Blue = 4}let c: Color = Color.Green;let colorName:string=Color[0]//‘Red’
void let unusable: void = undefined; It is useless to declare a variable of type void, because it can only be assigned undefined and null
object object represents a non-primitive type, that is, a type other than number, string, boolean, symbol, null or undefined. For example: declare function create(o: object | null): void;create({ prop: 0 }); // OK
symbol let sym2 = Symbol("key"); let sym3 = Symbol("key"); sym2 === sym3; // false, symbols are unique
  • Variable declaration
    let and const are new variable declaration methods in es6. In ts, let and const are used in exactly the same way as in es6. It is recommended that you use let and const for assignment. There are sometimes many weird places when using var declarations. For details, please refer to the official ts documentation.
//即便不指定类型,ts依旧可以根据指定的变量进行类型推断,如下所示:
    let name = `hello world`
    const age = 5
//如果在初始化阶段已经声明了该变量类型,在中途更改,会触发ts的编译时报错。
  • Type assertion
    Sometimes we encounter another situation where we know the type of this value better than ts. Through type assertion, we can clearly tell the compiler what we want to do. Therefore, type assertions are often used to circumvent the compiler's type checking function.
断言分为两种形式,第一种用“尖括号”:
let str:any="this is a string"
let strLength:number = (<string>str).length;

另外一种使用关键字as:
let str:any="this is a string"
let strLength:number = (str as string).length;
  • Generic
function identity<T>(arg: T): T {
    return arg;
}

定义了泛型函数后,可以用两种方法使用。 

第一种是,传入所有的参数,包含类型参数:
let output = identity<string>("myString");  // type of output will be 'string'
这里我们明确的指定了T是string类型,并做为一个参数传给函数,使用了<>括起来而不是()。

第二种方法更普遍。利用了类型推论 -- 即编译器会根据传入的参数自动地帮助我们确定T的类型:
let output = identity("myString");  // type of output will be 'string'
  • Cross type and joint type
基本类型是不会存在交叉的。比如number和string是不可能有交叉点的,一个类型不可能既是字符串又是数字。所以当我们使用交叉类型时通常时下面这样:
interface A {
    d:number,
    z:string
}

interface B {
    d:string,
    g:string
}
type C = A&B; 
let c:C
//使用交叉类型时,需要确保属性不会发生冲突,例如d既是number类型又是string类型,因此d时不能通过类型检查的。
interface A {
    d:string,
    z:string
}

interface B {
    d:string,
    g:string
}
//如果一个值是联合类型,我们只能访问它们共有的属性。

Guess you like

Origin blog.csdn.net/qq_36828822/article/details/106885192