1. Concept preview

  • Basic type:Boolean value.digital.string.An array of.tuples.The enumeration.any.void.null.undefined.never.object.
  • Type assertions: Sometimes we know more about a value than typescript does, and we can annotate it with assertions to tell the compiler that we know what WE’re doing. However, no special data review or deconstruction is performed.
  • Variable declaration:letwithconst

2. Code presentation

2.1 Basic Types

  • Boolean value
 let isDone: boolean = false;
Copy the code
  • digital
 let decLiteral: number = 6;
 let hexLiteral: number = 0xf00d;
 / / [ˈ ba ɪ n goes ri]
 let binaryLiteral: nubmer = 0b1010;
 let octalLiteral: number = 0o744;
Copy the code
  • string
let name: stirng = "bob";
name = "smith"
/* Template strings do not define multiple lines of text and inline expressions, use backquotes, and newlines are equivalent to \n\n*/
Copy the code
  • An array of
/* Single element type */
let list: number[] = [1.2.3];
/* Array generics */
let list: Array<number> = [1.2.3];
Copy the code
  • tuples
/*tuple*/
/* The tuple type allows you to represent an array of the number and type of elements */
/* Declare [dɪ t ɪ t ə r] a tuple [tjuple] type */
let x: [string.number];
x = ["hello".10];
x = [10."hello"];
/* When accessing an out-of-bounds element, the union type is used instead of */
x[3] = "world"; / / ok, strings can be copied to (string | number) type
Copy the code
  • The enumeration
/* enum types are a complement to the javaScript standard data, as enum types in C# and other languages can give more friendly names to sets of values */
/* Elements are numbered from 0 by default */
enum Color {Red,Green,Blue}
let c: Color = Color.Green;

/* We change Red to 1 to start numbering from 1 */
enum Color {Red = 1, Green, Blue}
let c: Color = Color.Green;

/* All manual assignments */
enum Color {Red = 1, Green = 2, Blue = 4}
let c: Color = Color.Green;

/* We know the value is 2, but we don't know the mapping name */
enum Color {Red = 1, Green = 2, Blue = 4}
let colorName: string = Color[2]; // 'Green'
Copy the code
  • any
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay,definely a boolean

let notSure: any = 4;
notSure.ifItExists(); // okay, ifItExists might exist at runtime
notSure.toFixed(); // okay, toFixed exists (but the compiler doesn't check)

let prettySure: Object = 4;
prettySure.toFixed(); // Error: Property 'toFixed' doesn't exist on type 'Object'.

// The any type is also useful when you know the type of part of the data.
let list: any[] = [1.true."free"];
list[1] = 100
Copy the code
  • void
// In a way, void is the opposite of any, indicating that there is no type at all.
Void is used when a function has no return value type
function warnUser() :void {
    console.log("This is my warning message");
}

// A void variable is useless because you can only give it undefined and null
let unusable: void = undefined;
Copy the code
  • null
// Not much else we can assign to these variables!
let n: null = null;
Copy the code
  • undefined
let u: undefined = undefined;
/ / void and (undefined | null) what's the difference
// null and undefined are subtypes of all classes.
// You can copy null and undefined to variables of type number

// However, when you specify strictNullChecks,
// The e of null and undefined is copied to void and their own.
// This avoids many common problems.
// Maybe somewhere you want to pass in a string or null or undefined,
/ / you can use the combined type string | null | is undefined

StrictNullChecks we manage to use strictNullChecks whenever possible. In this manual we assume that this flag is closed.
Copy the code
  • never
// The never type represents the types of values that never exist.
// The never type is the return type of function expressions or arrow function expressions that always throw an exception or return no value at all;
// Variables can also be never, when they are bound by type protection that is never true;

// A function that returns never must have an unreachable endpoint
function error(message: string) :never {
    throw new Error(message);
}

// The inferred return value type is never
function fail() {
    return error("Something failed");
}

// A function that returns never must have an unreachable endpoint
function infiniteLoop() :never {
    while (true) {}}Copy the code
  • Object

Object is a primitive type, that is, in addition to number, string, Boolean, symbol, the type of null or undefined Use object type, it can be better said as the object. The create such apis. For example,

declare function create(o:object| null) :void;
create({prop:0}); // OK
create(null); // OK
create(42); // Error
create("string") // Error
create(false) // Error
create(undefined) // Erroir
Copy the code

2.2 Type Inference

  • Assertions take two forms
/* Angle bracket syntax */
let someValue: any = "this is a string";

let strLength:number = (<string>someValue).length;

/* as syntax */
let someValue: any = "this is a string";

let strLength:number = (someValue as string).length;
Copy the code

2.3 Variable Declaration

var a = 10;
// Const is an enhancement to let that prevents reassignment of a variable

// And catch variable oddities
function f(shouldInitialize: boolean) {
    if (shouldInitialize) {
        var x = 10;
    }

    return x;
}

f(true);  // returns '10'
f(false); // returns 'undefined'

for (var i = 0; i < 10; i++) {
    setTimeout(function() { console.log(i); }, 100 * i);
}

// This can also be done by executing functions immediately

// The let variable uses lexical and block scope
// Block-scoped side waves are not accessible outside the block or for loop that contains them
Copy the code