Learn codind with amol
PART= 14 TypeScript
hello my name is amol sharma or vineet and this is my 14th part of learn coding with amol and in this part we are going TypeScript basics to advance.
Complete TypeScript Guide (Beginner → Intermediate)
1. First TypeScript Program
Definition: Declare a variable with a type. `let` declares a block-scoped variable. TypeScript uses `: type` after a variable name to set its static type.
let message: string = "Hello TypeScript";
console.log(message);
Compile with tsc filename.ts to get JavaScript.
2. Basic Data Types
Definition: Types tell the compiler what kind of values a variable can hold. Common types:
string, number, boolean, any, and arrays like number[].
let username: string = "Amol";
let age: number = 17;
let isStudent: boolean = true;
let numbers: number[] = [1, 2, 3];
let data: any = "Can be anything"; // 'any' disables type checks
3. Functions
Definition: Use `function name(params: type): returnType {}`. Parameter types and return type help prevent wrong usage.
function add(a: number, b: number): number {
return a + b;
}
console.log(add(5, 10));
4. Arrow Functions
Definition: Shorter function syntax. Types for parameters and return type are written the same way.
const multiply = (x: number, y: number): number => x * y;
console.log(multiply(4, 5));
5. Interface
Definition: `interface` defines the shape of an object (which properties and types it must have). Helpful for consistent object types.
interface Student {
name: string;
age: number;
isActive?: boolean; // ? means optional
}
let s1: Student = {
name: "Amol",
age: 17,
isActive: true
};
6. Class & Constructor Shorthand
Definition: `class` creates objects with properties and methods. Using `public` in the constructor parameters declares and initializes the property in one step.
class Person {
constructor(public name: string) {}
greet() {
console.log("Hello " + this.name);
}
}
const p = new Person("Amol");
p.greet();
7. Access Modifiers (public / private / protected)
Definition: Control visibility of class members. `public` accessible everywhere, `private` only inside the class, `protected` inside class and subclasses.
class User {
public name: string;
private password: string;
protected role: string;
constructor(name: string, password: string) {
this.name = name;
this.password = password;
this.role = "user";
}
}
8. Enums
Definition: `enum` gives friendly names to numeric values. Useful for a set of related constants.
enum Direction {
Up,
Down,
Left,
Right
}
let move: Direction = Direction.Up;
9. Tuples
Definition: Tuple is an array with fixed length and known types at each position.
let user: [string, number];
user = ["Amol", 17];
10. Type Aliases & Union Types
Definition: `type` creates an alias for a type. `|` creates a union of multiple possible types.
type ID = string | number;
let userId: ID = 101;
let value: string | number;
value = "hello";
value = 42;
11. Generics
Definition: Generics allow you to write reusable functions and types that work with different types while keeping type safety.
function identity(arg: T): T { return arg; } console.log(identity ("TypeScript")); console.log(identity (100));
12. Modules (export / import)
Definition: Break code into files. Use `export` to share and `import` to use exported values in other files.
// file1.ts
export function hello() {
console.log("Hello");
}
// file2.ts
import { hello } from "./file1";
hello();
13. Common Errors & Tips
Tips: Enable
strict in tsconfig.json for best checks. Use interfaces for object shapes and avoid any when possible.
Example tsconfig.json minimal snippet:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"strict": true,
"outDir": "dist"
}
}
, , or ?
© learncodingwithamol.blogspot.com

Comments
Post a Comment