var item1 = { id : 1 , name : "item1" } as any ; item1 = { id : 2 } ; let index = 0 ; if ( index == 0 ) { let index = 2 ; console . log ( `index = ${ index }` ) ; } console . log ( `index = ${ index }` ) ; const constValue = "this should not be changed" ; function printObject ( obj ) { console . log ( `obj = ${ obj }` ) ; } printObject ( 1 ) ; printObject ( "string value" ) ; function addWithUnion ( arg1 , arg2 ) { } function addWithTypeGuard ( arg1 , arg2 ) { if ( typeof arg1 === "string" ) { console . log ( `arg1 is of type string` ) ; return arg1 + arg2 ; } if ( typeof arg1 === "number" && typeof arg2 === "number" ) { console . log ( `arg1 and arg2 are numbers` ) ; return arg1 + arg2 ; } console . log ( `default return treat both as strings` ) ; return arg1 . toString ( ) + arg2 . toString ( ) ; } console . log ( ` "1", "2" = ${ addWithTypeGuard ( "1" , "2" ) }` ) ; console . log ( `  1 ,  2  = ${ addWithTypeGuard ( 1 , 2 ) }` ) ; console . log ( `  1 , "2" = ${ addWithTypeGuard ( 1 , "2" ) }` ) ; type StringOrNumber = string | number ; function addWithTypeAlias ( arg1 , arg2 ) { return arg1 . toString ( ) + arg2 . toString ( ) ; } enum DoorState { Open , Closed } function checkDoorState ( state ) { console . log ( `enum value is : ${ state }` ) ; switch ( state ) { case DoorState . Open : console . log ( `Door is open` ) ; break ; case DoorState . Closed : console . log ( `Door is closed` ) ; break ; } } checkDoorState ( DoorState . Open ) ; checkDoorState ( DoorState . Closed ) ; enum DoorStateSpecificValues { Open = 3 , Closed = 7 , Unspecified = 256 } enum DoorStateString { OPEN = "Open" , CLOSED = "Closed" } console . log ( `OPEN = ${ DoorStateString . OPEN }` ) ; const enum DoorStateConst { Open = 10 , Closed = 20 } console . log ( `const Closed = ${ DoorStateConst . Open }` ) ; let array = [ "123" , "456" , "789" ] ; delete array [ 0 ] ; for ( let i = 0 ; i < array . length ; i ++ ) { checkAndPrintElement ( array [ i ] ) ; } function checkAndPrintElement ( arrElement ) { if ( arrElement === undefined ) console . log ( `invalid array element` ) ; else console . log ( `valid array element : ${ arrElement }` ) ; } function printValues ( a ) { console . log ( `a = ${ a }` ) ; } printValues ( 1 ) ; printValues ( null ) ; const value = 10 ; const message = value > 10 ? "value is larger than 10" : "value is 10 or less" ; console . log ( message ) ; let objectA = { nestedProperty : { name : "nestedPropertyName" } } ; function printNestedObject ( obj ) { if ( obj != undefined && obj . nestedProperty != undefined && obj . nestedProperty . name ) { console . log ( `name = ${ obj . nestedProperty . name }` ) ; } else { console . log ( `name not found or undefined` ) ; } } printNestedObject ( null ) ; printNestedObject ( undefined ) ; printNestedObject ( { aProperty : "another property" } ) ; printNestedObject ( { nestedProperty : { name : "nestedPropertyName" } } ) ; function printNestedOptionalChain ( obj ) { if ( obj ?. nestedProperty ?. name ) { console . log ( `name = ${ obj . nestedProperty . name }` ) ; } else { console . log ( `name not found or undefined` ) ; } } printNestedOptionalChain ( undefined ) ; printNestedOptionalChain ( { aProperty : "another property" } ) ; printNestedOptionalChain ( { nestedProperty : { name : null } } ) ; printNestedOptionalChain ( { nestedProperty : { name : "nestedPropertyName" } } ) ; function nullishCheck ( a ) { console . log ( `a : ${ a ?? `undefined or null` }` ) ; } nullishCheck ( 1 ) ; nullishCheck ( null ) ; nullishCheck ( undefined ) ; function testNullOperands ( a , b ) { let addResult = a + ( b ?? 0 ) ; } var lValue = 2 ; let globalString ! ; setGlobalString ( "this string is set" ) ; console . log ( `globalString = ${ globalString }` ) ; function setGlobalString ( value ) { globalString = value ; } let structuredObject = { name : "myObject" , properties : { id : 1 , type : "AnObject" } } ; function printObjectType ( a ) { console . log ( `a: ${ JSON . stringify ( a ) }` ) ; } printObjectType ( structuredObject ) ; let a = "test" ; let aNumber = 2 ; aNumber = a ; let u = "an unknown" ; u = 1 ; let aNumber2 ; aNumber2 = < number > u ; console . log ( `aString : ${ a * 2 }` ) ; function alwaysThrows ( ) { throw new Error ( "this will always throw" ) ; } enum AnEnum { FIRST , SECOND } function getEnumValue ( enumValue ) { switch ( enumValue ) { case AnEnum . FIRST : return "First Case" ; case AnEnum . SECOND : return "Second Case" ; } let returnValue = enumValue ; return returnValue ; } let firstObj = { id : 1 , name : "firstObj" } ; let secondObj = { ... firstObj } ; console . log ( `secondObj : ${ JSON . stringify ( secondObj ) }` ) ; let nameObj = { name : "nameObj name" } ; let idObj = { id : 1 } ; let obj3 = { ... nameObj , ... idObj } ; console . log ( `obj3 = ${ JSON . stringify ( obj3 ) }` ) ; let objPrec1 = { id : 1 , name : "obj1 name" } ; let objPrec2 = { id : 1001 , desc : "obj2 description" } ; let objPrec3 = { ... objPrec1 , ... objPrec2 } ; console . log ( `objPrec3 : ${ JSON . stringify ( objPrec3 , null , 4 ) }` ) ; let firstArray = [ 1 , 2 , 3 ] ; let secondArray = [ 3 , 4 , 5 ] ; let thirdArray = [ ... firstArray , ... secondArray ] ; console . log ( `third array = ${ thirdArray }` ) ; let objArray1 = [ { id : 1 , name : "first element" } , ] ; let objArray2 = [ { id : 2 , name : "second element" } ] ; let objArray3 = [ ... objArray1 , { id : 3 , name : "third element" } , ... objArray2 ] ; console . log ( `objArray3 = ${ JSON . stringify ( objArray3 , null , 4 ) }` ) ; let tuple1 ; tuple1 = [ "test" , true ] ; console . log ( `tuple1[0] : ${ tuple1 [ 0 ] }` ) ; console . log ( `tuple1[1] : ${ tuple1 [ 1 ] }` ) ; let [ tupleString , tupleBoolean ] = tuple1 ; console . log ( `tupleString = ${ tupleString }` ) ; console . log ( `tupleBoolean = ${ tupleBoolean }` ) ; let tupleOptional ; tupleOptional = [ "test" , true ] ; tupleOptional = [ "test" ] ; console . log ( `tupleOptional[0] : ${ tupleOptional [ 0 ] }` ) ; console . log ( `tupleOptional[1] : ${ tupleOptional [ 1 ] }` ) ; let tupleRest ; tupleRest = [ 1 ] ; tupleRest = [ 1 , "string1" ] ; tupleRest = [ 1 , "string1" , "string2" ] ; let complexObject = { aNum : 1 , bStr : "name" , cBool : true } ; let { aNum , bStr , cBool } = complexObject ; console . log ( `aNum : ${ aNum }` ) ; console . log ( `bStr : ${ bStr }` ) ; console . log ( `cBool : ${ cBool }` ) ; let { aNum : objId , bStr : objName , cBool : isValid } = complexObject ; console . log ( `objId : ${ objId }` ) ; console . log ( `objName : ${ objName }` ) ; console . log ( `isValid : ${ isValid }` ) ;