This is the 11th day of my participation in Gwen Challenge

Type assertion:

Type assertions can be used to manually specify the type of a value, specifying a variable of a combined type to a more specific type.

/**
* params: name: string, age: number
* return age:number
*/
function fun1(name:string,age:number) :number {
    return age
}

let age1 = fun1('Joe'.18)

console.log(age);

Copy the code

2. Function types

If the parameters of a function are uncertain, you can define dynamic parameters by using extended operators.

function fun2(name:string,age:number,... args:any) :any {
    return args
}

let age1 = fun2('Joe'.18.'3434'.34.13231)

console.log(age1);
Copy the code

1. Add =’xx’ after the type to set the default value

function fun3(name:string='Cathy',age:number=20. args:any) :any {
    return age
}

let age2 = fun3()

console.log(age2);
Copy the code

2. Used in ES6

let fun4: (name:string,age:number) = >  number=function(name:string='Joe',age:number=20) :number {
        return age 
}

let age4 = fun4('Cathy'.18)
console.log(age4);
Copy the code

3. Define types in interface mode

interface fun5Interface {
    (name: string, age: number):number
}

let fun5:fun5Interface = function(name,age) {return age}

let age5 = fun5('zhao 6'.17)

console.log(age5);
Copy the code

4. Function overload

// 5. Function overload
function getValue(value:string) :string
function getValue(value:number) :number

function getValue(value:string|number) :string|number {
    return value
}

let v:string = getValue('555')
console.log(v);
Copy the code

5. There is no return value

// 6. No return value void
function getName(name: string) :void {
    console.log(name);
    
}
getName('zhao seven')
Copy the code

6. Execute only when the assertion is of a certain type

/ / assertions
function getAssert(value: string|number) {
    return (<string>value).length
}

console.log(getAssert('Joe'));
console.log(getAssert(18));
Copy the code

7. Assertions use good support in JSX

/ / assertions
function getAssert(value: string|number) {
    return (value as string).length
    // return (<string>value).length
}

console.log(getAssert('Joe'));
console.log(getAssert(18));

Copy the code

Type aliases

Definition 1.

A type alias can give a type a new name, defined by the type keyword, and can set string and numeric types.

  • Constraint type
// type Specifies the type alias
type strType = string | number
let str:strType = Awesome!
console.log(str);
Copy the code

  • Constraint string
// type Specifies the constraint string
type strType2 = 'male' | 'woman'
function getGender(sex: strType2) :string {
    return sex
}

let sex = getGender('male')
Copy the code

Fourth, the enumeration

Definition 1.

The enum type is a complement to the standard javascript data types. It is used to take values within a certain range. It defines a set of values.

  • example
enum Days{
    Sun,
    Mon,
    Tue,
    Web,
    Thu,
    Fri,
    Sat
}

console.log(Days);
// A value in the enumeration
console.log(Days[0]);

// enumerator
console.log(Days.Sun);

Copy the code

  • Example bidirectional mapping, self – increasing
enum Colors{
    red,
    gree=11,
    blue
}

/ / on the sex
console.log(Colors);
Copy the code