Skip to content

Commit 2fe74e4

Browse files
committed
types
1 parent 50edf65 commit 2fe74e4

File tree

7 files changed

+432
-10
lines changed

7 files changed

+432
-10
lines changed

.vscode/launch.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
8+
{
9+
"type": "node",
10+
"request": "launch",
11+
"name": "Launch Program",
12+
"skipFiles": [
13+
"<node_internals>/**"
14+
],
15+
"program": "${workspaceFolder}\\ts\\index.ts",
16+
"preLaunchTask": "tsc: build - tsconfig.json",
17+
"outFiles": [
18+
"${workspaceFolder}/**/*.js"
19+
]
20+
}
21+
]
22+
}

dist/index.js

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ a="coding techniques";
33

44
function add(a,b){ //no type checking
55
return a+b;
6-
}
6+
}
7+
const x=add(1,2);
8+
console.log(x);

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ts/index.ts

Lines changed: 278 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,286 @@
1-
let a1: number = 1; //syntax for type
2-
//a1="coding techniques"; //error //statically typed lang
3-
a1=10; //no error
1+
// Transpilation: Converting TypeScript (TS) code into JavaScript (JS) code
42

5-
let a2: string ="hi" //type-string
3+
// Example of Type Annotation for a variable
4+
let a1: number = 1; // 'a1' is explicitly declared as a number type
65

6+
// a1 = "coding techniques"; // Error: Type mismatch (statically typed language)
7+
a1 = 10; // Valid assignment since 'a1' is of type number
78

8-
//syntax for function and mentioning type
9+
// let a2: string = "hi"; // Example of a string type annotation
910

10-
function add1 (a : number , b : number ) : number {
11-
12-
return a + b; //type checking
11+
// Function with Type Annotations for parameters and return type
12+
function add1(a: number, b: number): number {
13+
return a + b; // Ensures the return type matches the defined type (number)
1314
}
1415

15-
add1(1,2);
16+
// Calling the function
17+
const y = add1(1, a1);
18+
console.log(y); // Output: 3
19+
20+
21+
a1=y;
22+
console.log(a1);
23+
24+
//Basic types
25+
26+
//`number`: Represents numeric values, both integer and floating point.
27+
let num : number=1233_456_789;
28+
console.log(num);
29+
30+
31+
//`bigint`: Represents large integers beyond the `number` type.
32+
let num1 : bigint =123456879n;
33+
console.log(num1);
34+
35+
// `string`: Represents textual data.
36+
let character:string = "Typescript";
37+
console.log(character);
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
/*
58+
59+
60+
// ==============================
61+
// 🚀 Built-in Types in TypeScript
62+
// ==============================
63+
64+
// ==============================
65+
// 📚 1. Primitive Types
66+
// ==============================
67+
68+
/*
69+
1. `number`
70+
*/
71+
72+
/*
73+
2. `string`
74+
*/
75+
76+
77+
78+
/*
79+
3. `boolean`: Represents a true or false value.
80+
Example: let isActive: boolean = true;
81+
*/
82+
83+
// let isActive: boolean = true; // Example: boolean
84+
85+
/*
86+
4. `null`: Represents intentional absence of a value.
87+
Example: let empty: null = null;
88+
*/
89+
90+
// let empty: null = null; // Example: null
91+
92+
/*
93+
5. `undefined`: Represents a declared variable without a value assigned.
94+
Example: let uninitialized: undefined;
95+
*/
96+
97+
// let uninitialized: undefined; // Example: undefined
98+
99+
/*
100+
6. `symbol`: Represents a unique identifier.
101+
Example: let sym: symbol = Symbol("id");
102+
*/
103+
104+
// let sym: symbol = Symbol("id"); // Example: symbol
105+
106+
/*
107+
7. `bigint`
108+
*/
109+
110+
// ==============================
111+
// 📚 2. Object Types
112+
// ==============================
113+
114+
/*
115+
1. `object`: Represents any non-primitive type (anything except `number`, `string`, `boolean`, `null`, `undefined`, `symbol`, `bigint`).
116+
Example: let person: object = { name: "John", age: 30 };
117+
*/
118+
119+
// let person: object = { name: "John", age: 30 }; // Example: object
120+
121+
/*
122+
2. `array`: Represents an ordered list of elements of a specific type.
123+
Example: let numbers: number[] = [1, 2, 3, 4];
124+
*/
125+
126+
// let numbers: number[] = [1, 2, 3, 4]; // Example: array
127+
128+
/*
129+
3. `function`: Represents a callable function.
130+
Example: let greet: (name: string) => string = (name) => "Hello, " + name;
131+
*/
132+
133+
// let greet: (name: string) => string = (name) => "Hello, " + name; // Example: function
134+
135+
/*
136+
4. `tuple`: Represents a fixed-size, ordered collection of elements of possibly different types.
137+
Example: let point: [number, number] = [10, 20];
138+
*/
139+
140+
// let point: [number, number] = [10, 20]; // Example: tuple
141+
142+
// ==============================
143+
// 📚 3. Special Types
144+
// ==============================
145+
146+
/*
147+
1. `any`: Represents any type, disabling type checking. Used when the type is unknown or not important.
148+
Example: let anything: any = "Hello"; anything = 42;
149+
*/
150+
151+
// let anything: any = "Hello"; anything = 42; // Example: any
152+
153+
/*
154+
2. `unknown`: Represents any type but requires type checking before using the value.
155+
Example: let something: unknown = "Hello";
156+
if (typeof something === "string") { console.log(something.length); }
157+
*/
158+
159+
// let something: unknown = "Hello"; if (typeof something === "string") { console.log(something.length); } // Example: unknown
160+
161+
/*
162+
3. `never`: Represents the type of values that never occur (e.g., functions that throw errors or have infinite loops).
163+
Example: function throwError(): never { throw new Error("Something went wrong!"); }
164+
*/
165+
166+
// function throwError(): never { throw new Error("Something went wrong!"); } // Example: never
167+
168+
/*
169+
4. `enum`: Represents a set of named constants, making it easier to work with fixed sets of values.
170+
Example: enum Direction { Up, Down, Left, Right } let move: Direction = Direction.Up;
171+
*/
172+
173+
// enum Direction { Up, Down, Left, Right } let move: Direction = Direction.Up; // Example: enum
174+
175+
// ==============================
176+
// 📚 4. Special Object Types
177+
// ==============================
178+
179+
/*
180+
1. `Date`: Represents a date object, used for date and time manipulation.
181+
Example: let today: Date = new Date();
182+
*/
183+
184+
// let today: Date = new Date(); // Example: Date
185+
186+
/*
187+
2. `RegExp`: Represents regular expressions for pattern matching in strings.
188+
Example: let pattern: RegExp = /abc/;
189+
*/
190+
191+
// let pattern: RegExp = /abc/; // Example: RegExp
192+
193+
// ==============================
194+
// 📚 5. Function Types
195+
// ==============================
196+
197+
/*
198+
1. Function Type: Defines the signature of a function with parameters and return type.
199+
Example: let add: (a: number, b: number) => number = (a, b) => a + b;
200+
*/
201+
202+
// let add: (a: number, b: number) => number = (a, b) => a + b; // Example: function type
203+
204+
205+
/*
206+
// ==============================
207+
// 🎯 3. Interview Questions & Answers
208+
// ==============================
209+
210+
// 1. **Difference between `interface` and `type`?**
211+
// - `interface`: Primarily for object shapes, can be extended or merged.
212+
// - `type`: More flexible, can define unions, intersections, etc., not limited to object shapes.
213+
214+
// 2. **What are generics?**
215+
// - Generics allow for type-safe functions or classes that work with any data type.
216+
function tsGeneric<T>(arg: T): T { return arg; } // Generic function example
217+
218+
// 3. **Difference between `any` and `unknown`?**
219+
// - `any`: No type checking, allows any type.
220+
// - `unknown`: Requires explicit type checking before usage.
221+
222+
// 4. **What is a type assertion in TypeScript?**
223+
// - Type assertions allow us to tell the compiler to treat a variable as a specific type, without changing the underlying value.
224+
let someValue: any = "Hello World";
225+
let strLength: number = (someValue as string).length; // Type assertion example
226+
227+
// 5. **What is the difference between `==` and `===`?**
228+
// - `==`: Checks for equality of values, performs type coercion.
229+
// - `===`: Checks for equality of both value and type, no type coercion.
230+
231+
// 6. **What are mapped types in TypeScript?**
232+
// - Mapped types allow you to create new types by transforming properties of an existing type.
233+
type ReadonlyPerson = { readonly [K in keyof tsPerson]: tsPerson[K] }; // Example of mapped type
234+
235+
// 7. **What are optional properties in TypeScript?**
236+
// - Properties that may or may not be provided when creating an object.
237+
interface tsProduct { name: string; price?: number; } // Optional property example
238+
239+
// 8. **How do TypeScript and JavaScript handle null and undefined?**
240+
// - In JavaScript, `null` is an object and `undefined` is a primitive value.
241+
// - In TypeScript, `null` and `undefined` are types, and strict null checks can be enforced.
242+
243+
244+
245+
// ==============================
246+
// 🎯 3. Interview Placement Questions
247+
// ==============================
248+
249+
250+
// 📝 TypeScript Interview Questions:
251+
// 1. **Differences between JavaScript and TypeScript?**
252+
// - **JavaScript**: Dynamic typing, no type annotations.
253+
// - **TypeScript**: Statically typed with support for interfaces, types, and other features.
254+
255+
// 2. **What is type inference?**
256+
// Type inference is when TypeScript automatically deduces the type of a variable based on its value or context, reducing the need for explicit type annotations.
257+
258+
// 3. **Difference between interface and type?**
259+
// - **Interface**: Defines the shape of an object, can be merged using declaration merging.
260+
// - **Type**: Defines the shape and can also be used to create unions or intersections.
261+
262+
// 4. **What are generics?**
263+
// Generics allow functions or classes to work with any type while preserving the type of the input and output values, making code reusable and type-safe.
264+
265+
// 5. **Explain optional properties.**
266+
// Optional properties in interfaces are defined using a `?`, meaning that they may or may not be present in an object.
267+
268+
// 6. **Difference between any and unknown?**
269+
// - **any**: Can hold any type and can be assigned to any type, bypassing type safety.
270+
// - **unknown**: Safer than **any**, requires type checking before performing operations.
271+
272+
// 7. What are mapped types?
273+
// Mapped types allow you to create new types by transforming properties of an existing type.
274+
275+
// 8. **Access modifiers: public, private, protected?**
276+
// - **public**: Accessible from anywhere.
277+
// - **private**: Accessible only within the class.
278+
// - **protected**: Accessible within the class and subclasses.
279+
280+
// 9. **Explain enums.**
281+
// Enums allow defining a set of named constants, often used for values that have a limited set of options.
16282
283+
// 10. **How does TypeScript handle null and undefined?**
284+
// TypeScript provides stricter handling of **null** and **undefined**. You can enable strict null checks to ensure that **null** and **undefined** are handled explicitly.
17285
286+
*/

0 commit comments

Comments
 (0)