𝗠𝗮𝘀𝘁𝗲𝗿 𝗧𝘆𝗽𝗲𝗦𝗰𝗿𝗶𝗽𝘁: 𝗣𝗮𝗿𝘁 𝟬𝟭
TypeScript તમારા કોડને રન (run) કરતા પહેલા તપાસે છે. જ્યારે તમે કોડ લખતા હોવ ત્યારે જ તે ભૂલો શોધી કાઢે છે. આનાથી બગ્સ (bugs) તમારા યુઝર્સ સુધી પહોંચતા અટકાવી શકાય છે.
Basic Types TypeScript ડેટા કેવો દેખાય છે તે વ્યાખ્યાયિત કરવા માટે પ્રકારો (types) નો ઉપયોગ કરે છે.
• String: let message: string = "Hello"; • Number: let age: number = 30; • Boolean: let isActive: boolean = true;
Arrays and Tuples • Arrays: let names: string[] = ["Alice", "Bob"]; • Tuples: let user: [number, string] = [1, "Alice"]; (અહીં ક્રમ મહત્વનો છે). • Enums: enum Role { Admin, User } (રેન્ડમ નંબર્સને બદલે નામ આપેલા વિકલ્પોનો ઉપયોગ કરો).
Objects and Functions ઓબ્જેક્ટ્સ સંબંધિત ડેટાને એકસાથે જૂથબદ્ધ કરે છે. • let car: { brand: string; year: number } = { brand: "Tesla", year: 2023 }; • Optional fields: કોઈ ફિલ્ડ ફરજિયાત નથી તે દર્શાવવા માટે ? નો ઉપયોગ કરો.
ફંક્શન્સને ઇનપુટ્સ અને આઉટપુટ્સ માટે પ્રકારોની જરૂર હોય છે. • function add(a: number, b: number): number { return a + b; } • Arrow functions: const multiply = (a: number, b: number): number => a * b;
Advanced Type Tools Type Aliases કોઈ શેપ (shape) માટે નામ બનાવો અને તેનો ફરીથી ઉપયોગ કરો. • type User = { id: number; name: string };
Unions and Intersections • Unions (OR): type ID = string | number; • Intersections (AND): type Employee = Person & { salary: number };
Generics
જનરિક્સ ઘણા પ્રકારો સાથે કામ કરવા માટે પ્લેસહોલ્ડર (placeholder) નો ઉપયોગ કરે છે.
• function wrap
કેવી રીતે શીખવું:
- દરેક ઉદાહરણ જાતે ટાઇપ કરો.
- જાણીજોઈને કોડમાં ભૂલ કરો (break the code).
- ભૂલો (errors) વાંચો.
- તેને સુધારો.
Part 02 માં એડવાન્સ્ડ ટાઇપ ટુલ્સ આવરી લેવામાં આવશે.
Source: https://dev.to/mdhemalakhand1999/master-typescript-part-01-452g