Skip to content

Commit 88893fd

Browse files
Ritik PrasadRitik Prasad
Ritik Prasad
authored and
Ritik Prasad
committed
Initial commit
0 parents  commit 88893fd

13 files changed

+958
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

1Datatypes.ts

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Basic types
2+
let myNumber: number = 42;
3+
let myString: string = "Hello, TypeScript!";
4+
let isDone: boolean = true;
5+
6+
// Arrays
7+
let numberArray: number[] = [1, 2, 3, 4];
8+
let stringArray: string[] = ["apple", "banana", "cherry"];
9+
let booleanArray: boolean[] = [true, false, true];
10+
11+
// const tsName: string | undefined = 'Ritik';
12+
13+
// let lowercase = tsName![0].toUpperCase();
14+
15+
16+
// in jS
17+
// const name = 'Ritik'; // Note: no type annotations
18+
19+
// let lowercase = name && name[0] ? name[0].toUpperCase() : undefined;
20+
21+
22+
// Tuples
23+
let myTuple: [number, string, boolean] = [1, "hello", true];
24+
25+
// Enum
26+
enum Color {
27+
Red,
28+
Green,
29+
Blue,
30+
}
31+
let myColor: Color = Color.Green;
32+
33+
// Any
34+
let anything: any = "Could be a string";
35+
anything = 42; // Now it's a number
36+
37+
// Unknown
38+
let notSure: unknown = 4;
39+
if (typeof notSure === "number") {
40+
let newNumber: number = notSure; // TypeScript knows it's a number here
41+
}
42+
43+
// Never
44+
function error(message: string): never {
45+
throw new Error(message);
46+
}
47+
48+
// Void
49+
function warnUser(): void {
50+
console.log("This is a warning message");
51+
}
52+
53+
// Null and Undefined
54+
let u: undefined = undefined;
55+
let n: null = null;
56+
57+
// BigInt
58+
let bigIntNumber: bigint = 9007199254740991n;
59+
60+
// Object
61+
let user: { name: string; age: number } = { name: "John", age: 30 };
62+
63+
// Type assertion
64+
let someValue: any = "this is a string";
65+
let strLength: number = (someValue as string).length;
66+
67+
// Union Types
68+
let unionType: string | number;
69+
unionType = "hello";
70+
unionType = 42;
71+
72+
// Intersection Types
73+
interface Person {
74+
name: string;
75+
}
76+
77+
interface Employee {
78+
employeeId: number;
79+
}
80+
81+
let employee: Person & Employee = { name: "Jane", employeeId: 1234 };
82+
83+
// Function with parameter and return type annotations
84+
// function add(a: number, b: number): number {
85+
// return a + b;
86+
// }
87+
88+
// Interface
89+
interface Point {
90+
x: number;
91+
y: number;
92+
}
93+
94+
let point: Point = { x: 10, y: 20 };
95+
96+
// Type alias
97+
type StringOrNumber = string | number;
98+
99+
let sample: StringOrNumber;
100+
sample = "hello";
101+
sample = 123;
102+
103+
// Optional properties
104+
interface Rectangle {
105+
width: number;
106+
height?: number; // optional property
107+
}
108+
109+
let rect1: Rectangle = { width: 100 };
110+
let rect2: Rectangle = { width: 100, height: 50 };
111+
112+
// Readonly properties
113+
interface Circle {
114+
readonly radius: number;
115+
}
116+
117+
let circle: Circle = { radius: 10 };
118+
// circle.radius = 20; // Error: Cannot assign to 'radius' because it is a read-only property.

Assertions.ts

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
2+
// Type assertions in TypeScript are a way to tell the compiler to treat a value as a specific type.
3+
// They don’t perform any runtime checks or
4+
// type conversions but are useful for cases where you, as the developer,
5+
// have more information about the type than TypeScript’s type inference system.
6+
7+
// Syntax
8+
// TypeScript provides two syntaxes for type assertions:
9+
10+
// Angle-Bracket Syntax: (value as Type)
11+
// as Keyword Syntax: <Type>value
12+
// Here’s how you can use both syntaxes:
13+
14+
// let someValue: any = "This is a string";
15+
16+
// // Using 'as' syntax
17+
// let strLength1: number = (someValue as string).length;
18+
19+
// // Using angle-bracket syntax
20+
// let strLength2: number = (<string>someValue).length;
21+
22+
// console.log(strLength1); // Output: 16
23+
// console.log(strLength2); // Output: 16
24+
25+
// Example 2: Assertions with DOM Manipulation
26+
// Sometimes, when working with the DOM, you might need to assert that an element has a certain type.
27+
28+
29+
// let myInput = document.getElementById("myInput") as HTMLInputElement;
30+
// myInput.value = "Hello, TypeScript!";
31+
32+
// let anotherInput = <HTMLInputElement>document.getElementById("anotherInput");
33+
// anotherInput.value = "Another input";
34+
// Example 3: Assertions with Custom Types
35+
// You can also use type assertions with custom types or interfaces.
36+
37+
38+
// interface Person {
39+
// name: string;
40+
// age: number;
41+
// }
42+
43+
// let personData: any = {
44+
// name: "Alice",
45+
// age: 30
46+
// };
47+
48+
// // Using 'as' syntax
49+
// let person1: Person = personData as Person;
50+
51+
// // Using angle-bracket syntax
52+
// let person2: Person = <Person>personData;
53+
54+
// console.log(person1.name); // Output: Alice
55+
// console.log(person2.age); // Output: 30
56+
// Example 4: Narrowing Down Types
57+
// Type assertions can also help in narrowing down types when working with union types.
58+
59+
60+
// function printId(id: number | string) {
61+
// // Assert that id is a number
62+
// let idNumber: number = id as number;
63+
// console.log(idNumber.toFixed(2)); // Output depends on id's value
64+
// }
65+
66+
// // Example usage
67+
// printId(123); // Output: 123.00
68+
// printId("456"); // Output: Error at runtime, but TypeScript assumes id is a number
69+
// Example 5: Working with JSON
70+
// When parsing JSON data, you might want to assert the shape of the data.
71+
72+
73+
// interface User {
74+
// id: number;
75+
// name: string;
76+
// }
77+
78+
// let jsonData: any = '{"id": 1, "name": "Bob"}';
79+
// let user: User = JSON.parse(jsonData) as User;
80+
81+
// console.log(user.id); // Output: 1
82+
// console.log(user.name); // Output: Bob
83+
// Example 6: Conditional Type Assertions
84+
// You can use type assertions in combination with conditional checks to ensure type safety.
85+
86+
87+
// function handleResponse(response: any) {
88+
// if (response.success) {
89+
// let successData = response.data as { message: string };
90+
// console.log(successData.message);
91+
// } else {
92+
// console.log("Error:", response.error as string);
93+
// }
94+
// }
95+
96+
// // Example usage
97+
// handleResponse({ success: true, data: { message: "Operation successful" } });
98+
// handleResponse({ success: false, error: "Something went wrong" });
99+
// Summary
100+
// Type assertions let you override TypeScript’s type inference, asserting a value to be of a specific type.
101+
// They do not perform type checks or type conversions but are useful for scenarios where you know more about the type than TypeScript can infer.
102+
// Use angle-bracket syntax (<Type>value) or the as keyword syntax (value as Type) depending on your preference or coding standards.

Class.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// class Person {
2+
// // Public property
3+
// public name: string;
4+
5+
// // Private property
6+
// private age: number;
7+
8+
// // Protected property
9+
// protected address: string;
10+
11+
// // Readonly property
12+
// readonly birthDate: Date;
13+
14+
// // Constructor
15+
// constructor(name: string, age: number, address: string, birthDate: Date) {
16+
// this.name = name;
17+
// this.age = age;
18+
// this.address = address;
19+
// this.birthDate = birthDate;
20+
// }
21+
22+
// // Getter for age
23+
// public getAge(): number {
24+
// return this.age;
25+
// }
26+
27+
// // Setter for age
28+
// public setAge(age: number): void {
29+
// if (age > 0) {
30+
// this.age = age;
31+
// } else {
32+
// console.error("Age must be positive.");
33+
// }
34+
// }
35+
36+
// // Getter for address
37+
// protected getAddress(): string {
38+
// return this.address;
39+
// }
40+
41+
// // Setter for address
42+
// protected setAddress(address: string): void {
43+
// this.address = address;
44+
// }
45+
// }
46+
47+
// // Example usage
48+
// const person = new Person("John Doe", 30, "123 Main St", new Date("1990-01-01"));
49+
50+
// console.log(person.name); // Public property, accessible
51+
// console.log(person.getAge()); // Using getter for private property
52+
53+
// person.setAge(31); // Using setter for private property
54+
// console.log(person.getAge());
55+
56+
// // Accessing protected property or methods directly from instance is not possible
57+
// // console.log(person.address); // Error: Property 'address' is protected and only accessible within class 'Person' and its subclasses.
58+
// // console.log(person.getAddress()); // Error: Property 'getAddress' is protected and only accessible within class 'Person' and its subclasses.
59+
60+
// console.log(person.birthDate); // Readonly property, accessible but cannot be modified
61+
// // person.birthDate = new Date("2000-01-01"); // Error: Cannot assign to 'birthDate' because it is a read-only property.

Functions.ts

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Regular Functions
2+
3+
function add(a: number, b: number): number {
4+
return a + b;
5+
}
6+
7+
console.log(add(5, 3)); // Output: 8
8+
9+
// Function Expressions
10+
const multiply = function (a: number, b: number): number {
11+
return a * b;
12+
};
13+
14+
console.log(multiply(4, 5)); // Output: 20
15+
16+
// Arrow Functions
17+
const subtract = (a: number, b: number): number => {
18+
return a - b;
19+
};
20+
21+
console.log(subtract(10, 4)); // Output: 6
22+
23+
// Optional Parameters
24+
function greet(name: string, greeting?: string): string {
25+
return greeting ? `${greeting}, ${name}!` : `Hello, ${name}!`;
26+
}
27+
28+
console.log(greet("Alice")); // Output: Hello, Alice!
29+
console.log(greet("Alice", "Hi")); // Output: Hi, Alice!
30+
31+
// Default Parameters
32+
33+
function power(base: number, exponent: number = 2): number {
34+
return base ** exponent;
35+
}
36+
37+
console.log(power(3)); // Output: 9
38+
console.log(power(3, 3)); // Output: 27
39+
40+
// Rest Parameters
41+
42+
function sum(...numbers: number[]): number {
43+
return numbers.reduce((total, num) => total + num, 0);
44+
}
45+
46+
console.log(sum(1, 2, 3)); // Output: 6
47+
console.log(sum(4, 5, 6, 7)); // Output: 22
48+
49+
// Function Overloading
50+
51+
function display(value: string): void;
52+
function display(value: number): void;
53+
function display(value: string | number): void {
54+
console.log(`Value: ${value}`);
55+
}
56+
57+
display("Hello"); // Output: Value: Hello
58+
display(42); // Output: Value: 42
59+
60+
// Function Types
61+
type Operation = (a: number, b: number) => number;
62+
63+
// const add: Operation = (a, b) => a + b;
64+
// const multiply: Operation = (a, b) => a * b;
65+
66+
// console.log(add(5, 3)); // Output: 8
67+
// console.log(multiply(4, 5)); // Output: 20
68+
69+
// Callback Functions
70+
71+
function fetchData(url: string, callback: (data: string) => void): void {
72+
// Simulate fetching data
73+
const data = "Sample data from " + url;
74+
callback(data);
75+
}
76+
77+
fetchData("https://api.example.com", (data) => {
78+
console.log(data); // Output: Sample data from https://api.example.com
79+
});
80+
81+
// // Generic Functions
82+
// function identity<T>(arg: T): T {
83+
// return arg;
84+
// }
85+
86+
// console.log(identity<string>("Hello")); // Output: Hello
87+
// console.log(identity<number>(42)); // Output: 42

0 commit comments

Comments
 (0)