1+ console . log ( 'Hello World!' ) ;
2+
3+
4+ // https://www.w3schools.com/typescript/typescript_simple_types.php
5+
6+ let isActive : boolean = true ;
7+ let hasPermission = false ; // TypeScript infers 'boolean' type
8+
9+ let decimal : number = 6 ;
10+ let hex : number = 0xf00d ; // Hexadecimal
11+ let binary : number = 0b1010 ; // Binary
12+ let octal : number = 0o744 ; // Octal
13+ let float : number = 3.14 ; // Floating point
14+
15+ let color : string = "blue" ;
16+ let fullName : string = 'John Doe' ;
17+ let age : number = 30 ;
18+ let sentence : string = `Hello, my name is ${ fullName } and I'll be ${ age + 1 } next year.` ;
19+
20+ const bigNumber : bigint = 9007199254740991n ;
21+ const hugeNumber = BigInt ( 9007199254740991 ) ; // Alternative syntax
22+
23+ // Explicit type annotations
24+ // String
25+ let greeting : string = "Hello, TypeScript!" ;
26+
27+ // Number
28+ let userCount : number = 42 ;
29+
30+ // Boolean
31+ let isLoading : boolean = true ;
32+
33+ // Array of numbers
34+ let scores : number [ ] = [ 100 , 95 , 98 ] ;
35+
36+ // Output the values
37+ console . log ( greeting ) ;
38+ console . log ( userCount ) ;
39+ console . log ( isLoading ) ;
40+ console . log ( scores ) ;
41+
42+
43+ // Function with explicit parameter and return types
44+ function greet ( name : string ) : string {
45+ return `Hello, ${ name } !` ;
46+ }
47+
48+ // TypeScript will ensure you pass the correct argument type
49+ greet ( "Alice" ) ; // OK
50+ //greet(42); // Error: Argument of type '42' is not assignable to parameter of type 'string'
51+
52+ // TypeScript infers the shape of the object
53+ const user = {
54+ name : "Alice" ,
55+ age : 30 ,
56+ isAdmin : true
57+ } ;
58+
59+ // TypeScript knows these properties exist
60+ console . log ( user . name ) ; // OK
61+ //console.log(user.email); // Error: Property 'email' does not exist
62+
63+ let u = true ;
64+ //u = "string"; // Error: Type 'string' is not assignable to type 'boolean'.
65+ //Math.round(u); // Error: Argument of type 'boolean' is not assignable to parameter of type 'number'.
66+
67+ let v : any = true ;
68+ v = "string" ; // no error as it can be "any" type
69+ Math . round ( v ) ; // no error as it can be "any" type
70+
71+
72+ const names : string [ ] = [ ] ;
73+ names . push ( "Dylan" ) ; // no error
74+ // names.push(3); // Error: Argument of type 'number' is not assignable to parameter of type 'string'.
75+
76+ const names2 : readonly string [ ] = [ "Dylan" ] ;
77+ //names2.push("Jack"); // Error: Property 'push' does not exist on type 'readonly string[]'.
78+ // try removing the readonly modifier and see if it works?
79+
80+ const numbers = [ 1 , 2 , 3 ] ; // inferred to type number[]
81+ numbers . push ( 4 ) ; // no error
82+ // comment line below out to see the successful assignment
83+ //numbers.push("2"); // Error: Argument of type 'string' is not assignable to parameter of type 'number'.
84+ let head : number = numbers [ 0 ] ; // no error
85+
86+
87+ // define our tuple
88+ let ourTuple : [ number , boolean , string ] ;
89+
90+ // initialize correctly
91+ ourTuple = [ 5 , false , 'Coding God was here' ] ;
92+
93+ // define our tuple
94+ let ourTuple2 : [ number , boolean , string ] ;
95+ // initialized incorrectly which throws an error
96+ //ourTuple2 = [false, 'Coding God was mistaken', 5];
97+
98+ const car : { type : string , model : string , year : number } = {
99+ type : "Toyota" ,
100+ model : "Corolla" ,
101+ year : 2009
102+ } ;
103+
104+ enum CardinalDirections {
105+ North ,
106+ East ,
107+ South ,
108+ West
109+ }
110+ let currentDirection = CardinalDirections . North ;
111+ // logs 0
112+ console . log ( currentDirection ) ;
113+ // throws error as 'North' is not a valid enum
114+ //currentDirection = 'North'; // Error: "North" is not assignable to type 'CardinalDirections'.
115+
116+ type CarYear = number
117+ type CarType = string
118+ type CarModel = string
119+ type Car = {
120+ year : CarYear ,
121+ type : CarType ,
122+ model : CarModel
123+ }
124+
125+ const carYear : CarYear = 2001
126+ const carType : CarType = "Toyota"
127+ const carModel : CarModel = "Corolla"
128+ const car2 : Car = {
129+ year : carYear ,
130+ type : carType ,
131+ model : carModel
132+ } ;
133+
134+ type Animal = { name : string } ;
135+ type Bear = Animal & { honey : boolean } ;
136+ const bear : Bear = { name : "Winnie" , honey : true } ;
137+
138+ type Status = "success" | "error" ;
139+ let response : Status = "success" ;
140+
141+ interface Rectangle {
142+ height : number ,
143+ width : number
144+ }
145+
146+ const rectangle : Rectangle = {
147+ height : 20 ,
148+ width : 10
149+ } ;
150+
151+ function printStatusCode ( code : string | number ) {
152+ console . log ( `My status code is ${ code } .` )
153+ }
154+ printStatusCode ( 404 ) ;
155+ printStatusCode ( '404' ) ;
156+
157+ // the `: number` here specifies that this function returns a number
158+ function getTime ( ) : number {
159+ return new Date ( ) . getTime ( ) ;
160+ }
161+
162+ function printHello ( ) : void {
163+ console . log ( 'Hello!' ) ;
164+ }
165+
166+ function multiply ( a : number , b : number ) {
167+ return a * b ;
168+ }
169+
170+ // the `?` operator here marks parameter `c` as optional
171+ function add ( a : number , b : number , c ?: number ) {
172+ return a + b + ( c || 0 ) ;
173+ }
174+
175+ function pow ( value : number , exponent : number = 10 ) {
176+ return value ** exponent ;
177+ }
178+
179+ function divide ( { dividend, divisor } : { dividend : number , divisor : number } ) {
180+ return dividend / divisor ;
181+ }
182+
183+ function add2 ( a : number , b : number , ...rest : number [ ] ) {
184+ return a + b + rest . reduce ( ( p , c ) => p + c , 0 ) ;
185+ }
186+
187+ type Negate = ( value : number ) => number ;
188+ // in this function, the parameter `value` automatically gets assigned the type `number` from the type `Negate`
189+ const negateFunction : Negate = ( value ) => value * - 1 ;
190+
191+
192+ let x : unknown = 'hello' ;
193+ console . log ( ( x as string ) . length ) ;
194+
195+ class Person {
196+ private name : string ;
197+
198+ public constructor ( name : string ) {
199+ this . name = name ;
200+ }
201+
202+ public getName ( ) : string {
203+ return this . name ;
204+ }
205+ }
206+
207+ const person = new Person ( "Jane" ) ;
208+ console . log ( person . getName ( ) ) ; // person.name isn't accessible from outside the class since it's private
209+
210+ function createPair < S , T > ( v1 : S , v2 : T ) : [ S , T ] {
211+ return [ v1 , v2 ] ;
212+ }
213+ console . log ( createPair < string , number > ( 'hello' , 42 ) ) ; // ['hello', 42]
214+
215+ // cast
216+ let xx : unknown = 'hello' ;
217+ console . log ( ( < string > xx ) . length ) ; // <> syntax does not seen to work in jsx (good choice of symbol?)
0 commit comments