TypeScript में TS2533 त्रुटि को कैसे ठीक करें: "Object is possibly 'null' or 'undefined'"
नमस्ते! मैं TypeScript में नया हूँ और उपयोगकर्ताओं की array के साथ काम करने के लिए एक सरल function लिखने की कोशिश कर रहा हूँ। मेरे पास एक User interface और एक function है जो id के आधार पर उपयोगकर्ता ढूंढता है:
```typescript
interface User {
id: number;
name: string;
email: string;
}
const users: User[] = [
{ id: 1, name: 'Alice', email: 'alice@example.com' },
{ id: 2, name: 'Bob', email: 'bob@example.com' }
];
function findUser(id: number): User {
return users.find(user => user.id === id);
}
const user = findUser(1);
console.log(user.name);
```
लेकिन compiler यह error देता है:
```
error TS2533: Object is possibly 'null' or 'undefined'.
```
यह error `console.log(user.name)` लाइन पर आता है। मैं समझता हूँ कि `find` `undefined` लौटा सकता है अगर उपयोगकर्ता नहीं मिलता, लेकिन मुझे यकीन है कि id=1 वाला उपयोगकर्ता मौजूद है। मैं TypeScript को सही तरीके से कैसे बताऊँ कि object निश्चित रूप से `null` या `undefined` नहीं है? क्या मैं इस check को बंद कर सकता हूँ? या मुझे code बदलना होगा? मैं आभारी रहूँगा।