Ts learning

The base type

Any type

Number type number

let num:number=32

  • Number object properties

    MAX_VALUE=> maximum Number MIN_VALUE=> minimum Number NAN=> non-numeric value NEGATIVE_INFINITY= negative infinity POSITIVE_INFINITY= positive infinity prototype= “Number” static property of the object. Gives you the ability to add properties and methods to objects. Constructor => returns a reference to the Number function that created this object.

  • Number object method

    ToFixed ()= specifies the number of decimal places toLocaleString() = ToString ()= > converts a number to a string valueOf() = returns the original numeric valueOf a number object

String type String

let name:string=’miya’

  • String object properties

    constructor length prototype

  • The method attributes

    CharAt ()= returns the character at the specified position, not found returns “charCodeAt()=” returns the Unicode encoding at the specified position. Concat ()= concatenates 2 strings indexOf()= returns the position at which the specified string first appears Replace ()= replace search()= Retrieve the value matching the regular expression Slice ()= extract the fragment of the string, Split ()=> Split string into string array stringvar.substr(start [, Substring (start,end)= returns the truncated content toLocaleLowerCase()=> Uppercase to lowercase ToLocaleUpperCase = uppercase toLowerCase()= the string changes toLowerCase

Boolean type Boolean

  • let flag:boolean=false

An array type

Use Array generics let arr:Array=[1,2]

  • methods

    concat() every() filter() forEach() indexOf() join() lastIndexOf() map() pop() push() reduce() reduceRight() reverse() shift() slice() some() sort() splice() toString() unshift()

tuples

  • Let x:[string,number]; x=[‘Runoob’,1]

The enumeration

  • Enumeration types are used to define the value set enum

    • enum Color {Red,Green,Blue}
    • let c: Color=Color.Blue

void

  • Identifies the type of return value, indicating that the method has no return value

    • function hello() :void{aleat(‘hello miya’)}

null

  • Missing object value

undefined

  • Initialize a variable to an undefined value

never

  • Never is a subtype of other types, including null and undefined, and represents values that never occur

TypeScript variable declarations

Let [variable name]:[type]

  • Let uname:string (no assignment, default initialization is undefined)

Types of assertions

  • Type assertions can be used to manually specify the type of a value, that is, to allow variables to change from one type to another

    • < type > value
    • Value as type

Type inference

Let num =32 num =’32’ console.log(num

The TypeScript function

Function return value

  • function fn():number{return 2121}

Parametric function

  • function fn(x:number,y:number){return x+y}

Optional parameters

  • function fn(x:string,y? :string){return x+y}

The default parameters

  • function fn(x:number,y:number=3){return x+y}

The remaining parameters

  • function fn(x:number,… y:number[]){return x+y.reduce((x,y)=>x+y}

Anonymous functions

  • Since the call

    • ((a, b) = > a + b) (1, 3)
  • Anonymous functions

    • let fn=(x:number,y:number)=>x+y

Recursive function

  • let factorials=(number)=>number<=0? 1:(number*factorials(number-1))

Arrow function

  • let factorials=(number)=>number<=0? 1:(number*factorials(number-1))