Chapter 2TypeScript~1 min read
Basic Types
TypeScript चे मूळ Types
TypeScript मध्ये variables declare करताना त्यांचा type सांगता येतो. एकदा type set केल्यावर त्या variable मध्ये वेगळ्या type चे value टाकणे error देतो.
Primitive types
typescript
// String
let name: string = "Avadhoot";
let city: string = 'Pune';
// Number (integer आणि float दोन्ही)
let age: number = 25;
let price: number = 99.99;
// Boolean
let isLoggedIn: boolean = true;
let isPremium: boolean = false;
// Type inference — explicitly लिहायची गरज नाही
let language = "Marathi"; // TypeScript ओळखतो: string
let count = 10; // ओळखतो: numberSpecial types
typescript
// null आणि undefined
let value: null = null;
let data: undefined = undefined;
// any — type checking बंद (वापरणे टाळा)
let anything: any = 42;
anything = "hello"; // ✅ — but defeats the purpose
// unknown — any पेक्षा safe
let userInput: unknown;
userInput = 5;
userInput = "hello";
// Use करण्यापूर्वी type check करावा लागतो
if (typeof userInput === "string") {
console.log(userInput.toUpperCase());
}
// void — function काहीही return करत नाही
function logMessage(msg: string): void {
console.log(msg);
}
// never — function कधीही return होत नाही
function throwError(msg: string): never {
throw new Error(msg);
}💡
`any` type वापरणे टाळा — त्याने TypeScript चे सगळे benefits नाहीसे होतात. `unknown` हा safer alternative आहे.
✅ Key Points — लक्षात ठेवा
- ▸string, number, boolean — basic types
- ▸Type inference — TypeScript स्वतः ओळखतो
- ▸any — type checking disable (avoid करा)
- ▸unknown — any पेक्षा safe
- ▸void — no return value
0/9 chapters पूर्ण