Typescript fundamental

1. Hello World!

TypeScript is a superset of JavaScript that adds static types to the language. Let's start with a simple "Hello World" program.

Example:

console.log("Hello, World!");

To run this:

  1. Save it as hello.ts.

  2. Compile it to JavaScript using the TypeScript compiler: tsc hello.ts.

  3. Run the generated hello.js file with Node.js: node hello.js.


2. Variables, Constants, and Data Types

TypeScript supports variables and constants with explicit type annotations. Common data types include number, string, boolean, any, and more.

Variables

Use let or const for variables. TypeScript infers types when not explicitly declared.

Example:

let name: string = "Alice";
let age: number = 25;
let isStudent: boolean = true;
let dynamic: any = "Can be anything";

Constants

Use const for values that won't change.

Example:

Other Data Types

  • null and undefined

  • object, array, tuple, enum, etc. (covered later).


3. Concatenation and Interpolation

You can combine strings using concatenation (+) or template literals for interpolation.

Concatenation:

Interpolation (using template literals):


4. Objects and Arrays

Objects

Objects in TypeScript can have defined types for their properties.

Example:

Arrays

Arrays hold multiple values of the same type.

Example:

Tuples

Fixed-length arrays with specific types.

Example:


5. Relational and Equality Operators

elational operators compare values, and equality operators check for equality.

Relational Operators

  • <, >, <=, >=

Example:

Equality Operators

  • == (loose equality, type coercion)

  • === (strict equality, no type coercion)

  • !=, !==

Example:

6. Logical Operators

Logical operators combine conditions: && (AND), || (OR), ! (NOT).

Example:


7. Conditional Statements

Conditional statements control the flow of execution based on conditions.

If-Else

Switch


8. Loops

Loops allow repetitive execution of code.

For Loop

While Loop

For...of Loop (for arrays)

9. Functions

Functions in TypeScript can have typed parameters and return types.

Example:

Optional Parameters

Arrow Functions


10. Classes and Methods

Classes define blueprints for objects with properties and methods.

Example:

Inheritance


11. Practice Exercises

Try these exercises to test your understanding of TypeScript fundamentals.

  1. Hello World Modification Create a program that prints "Welcome to TypeScript!" to the console.

  2. Variable Swap Declare two variables a and b with numbers, swap their values, and print the result.

  3. String Interpolation Create a function that takes a name and age and returns a string like "Hello, [name]! You are [age] years old."

  4. Array Sum Write a function that takes an array of numbers and returns their sum.

  5. Object Creation Define an object type for a Car with properties brand (string) and year (number). Create an instance and print its properties.

  6. Equality Check Write a function that checks if two numbers are equal using both == and === and explains the difference in output.

  7. Logical Operator Task Create a function that takes age and hasLicense and returns true only if the person is over 18 and has a license.

  8. Grade Calculator Write a program that takes a score (0-100) and prints a letter grade (A, B, C, D, F) using if-else or switch.

  9. Loop Exercise Write a loop that prints all even numbers from 1 to 20.

  10. Class Creation Create a Rectangle class with width and height properties and a method getArea() that returns the area. Test it with an instance.

Last updated