← Back to React Course | Chapter 13: Advanced React & Architecture | Lesson 8 of 14

TypeScript के साथ React का उपयोग

React के साथ TypeScript उपयोग करना आपकी move की हर box पर exactly लिखने जैसा है कि अंदर क्या है, ताकि कोई accidentally kitchen वाला box न खोले यह expect करते हुए कि उसमें bathroom supplies हैं।
Syntax
markup
interface ComponentProps {
  name: string;
  count?: number;
}

function Component({ name, count = 0 }: ComponentProps) {
  const [value, setValue] = useState<string>("");
  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => setValue(e.target.value);
  return <input value={value} onChange={handleChange} />;
}

Component Props को Type करना

एक interface (या type alias) define करना जो किसी component की expected props describe करे, फिर उसे props parameter के type के रूप में उपयोग करना, TypeScript को एक missing required prop या wrong type की value जैसी mistakes को compile time पर, code run होने से पहले ही पकड़ने देता है।

Note: Props interface का नाम component के बाद एक Props suffix के साथ रखें, जैसे एक Greeting component के लिए GreetingProps।
Warning: Props को any type करना (या types को पूरी तरह skip करना) उस component के लिए TypeScript का सारा benefit छोड़ देता है — यह essentially plain JavaScript जैसा वापस हो जाता है।

उदाहरण: Typing Component Props

markup
// Run in your local React project (npm install required)
interface GreetingProps {
  name: string;
  age?: number; // optional
}
function Greeting({ name, age }: GreetingProps) {
  return <p>Hello, {name}{age ? ` (${age})` : ''}</p>;
}

useState को Type करना

TypeScript आमतौर पर एक useState hook का type उसकी initial value से automatically infer करता है, जैसे useState(0) number के रूप में infer होता है। जब initial value intended type को पूरी तरह capture नहीं करती (जैसे एक empty array जो बाद में strings रखेगा), आप explicitly एक generic के साथ type specify करते हैं।

Note: useState<string[]>([]) एक common pattern है — TypeScript एक empty array से अकेले 'strings का array' infer नहीं कर सकता।
Warning: बिना explicit generic के, useState([]) type को never[] infer करता है, जो फिर आप बाद में add करने की कोशिश करने वाले किसी भी actual items को reject करता है।

उदाहरण: Typing useState

markup
// Run in your local React project (npm install required)
function TodoList() {
  const [todos, setTodos] = useState<string[]>([]);
  const addTodo = (text: string) => setTodos([...todos, text]);
  return <button onClick={() => addTodo('New task')}>Add ({todos.length})</button>;
}

Event Handlers को Type करना

React के built-in TypeScript types में specific event types शामिल हैं, जैसे एक input के onChange handler के लिए React.ChangeEvent<HTMLInputElement>, आपको event.target.value जैसी properties पर correct autocomplete और type-checking देते हुए।

Note: अपने editor के autocomplete को किसी given JSX event prop के लिए exact event type suggest करने दें — आपको शायद ही कभी इन सभी को याद रखना पड़े।
Warning: एक generic any या गलत specific event type (जैसे एक button का event type एक input पर उपयोग करना) real type errors hide कर सकता है जो related हैं कि वह event पर actually कौन सी properties मौजूद हैं।

उदाहरण: Typing Event Handlers

markup
// Run in your local React project (npm install required)
function SearchBox() {
  const [query, setQuery] = useState('');
  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    setQuery(e.target.value);
  };
  return <input value={query} onChange={handleChange} />;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक component की props को any type करना, जो TypeScript जोड़ने के पूरे purpose को defeat करता है।
  2. कुछ TS configurations में JSX.Element या React.FC return types के लिए ज़रूरी React import भूल जाना।
  3. useState का generic type न करना जब initial value पूरे type को clearly imply नहीं करती (जैसे एक list को empty array से शुरू करना)।
चैप्टर सारांश
  • TypeScript React में static type-checking जोड़ता है, कई prop/state mismatches runtime से पहले पकड़ते हुए।
  • एक component की props typically इसकी props object के लिए एक interface या type alias से typed होती हैं।
  • useState<Type>(initialValue) आपको explicitly एक hook का type specify करने देता है जब यह correctly infer नहीं हो सकता।
  • TypeScript सीधे React के existing patterns में integrate होता है — components, hooks, और props सब typed हो जाते हैं, replace नहीं होते।
ब्राउज़र सपोर्ट

एक TypeScript-aware build setup चाहिए (Vite/Next.js templates इसे out of the box support करते हैं); किसी भी React version के साथ काम करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.