typescript- learning to use ts-3

function

Function Arguments

  • Parameter and return type
function add(x: number, y: number): number {
    return x + y
}
  • Optional parameters
function add(x: number, y?: number): number {
    return x + 10
}
  • The default parameters
function add(x: number, y: number = 20): number {
    return x + y
}
  • The remaining parameters
function sum(...args: number[]): number {
    let ret: number = 0
    args.forEach((item: number): void => {
        ret += item
    })
    return ret
}

sum(1, 2, 3)

Arrow function

  • A basic example
let add = (x: number, y: number): number => x + y

for-of loop

  • for loop
  • forEach
    • It does not support the break
  • for in
    • As will an array of objects to traverse
  • for of
    • Support break

Type inference (Type Inference)

Type compatibility

Module

concept

Communication module: Export

export default xxx

export const foo: string = 'bar';
export const bar: string = 'foo';

Communicating: Import

// 加载默认成员
import xxx from '模块标识'

// 按需加载模块成员
import {foo, bar} from '模块'

TypeScript summary

  • What is TypeScript
  • Variable declaration
    • where
    • let
    • const
  • Basic data types
    • Boolean value boolean
    • digital number
    • String string
    • Array number[]orArray<number>
    • Originator [number, string]
    • Object object, you can understand
    • Any type any
    • Function returns an empty value void
    • null with undefined
  • interface
    • interface
  • Deconstruction assignment
    • An array of deconstruction
    • Object deconstruction
  • Operators Expand
    • Expand Arrays
    • Expand the object
  • class
    • The basic syntax
    • Constructor
    • inherit
    • Property modifiers
    • get and set properties
  • function
    • parameter
    • Arrow function
  • for-of loop
  • Module
    • Export
    • Importing

Guess you like

Origin www.cnblogs.com/ygjzs/p/12215478.html