Utility Types में NonNullable
In this page:
type Result = NonNullable<type | null | undefined>;
Basic NonNullable
NonNullable<T> एक type T से null और undefined हटाता है जबकि union के बाकी हर member preserve करता है, एक ऐसे type को narrow करते हुए जो nullable हो सकता था एक ऐसे में जो guaranteed न हो।
उदाहरण: Basic NonNullable
type MaybeString = string | null | undefined;
type DefiniteString = NonNullable<MaybeString>;
const s: DefiniteString = "hello";
console.log(s);
NonNullable with Functions
NonNullable उन functions के return type को clean कर सकता है जो otherwise null या undefined return कर सकते हैं, जो उपयोगी है एक बार caller ने nullable case को पहले ही handle या rule out कर दिया हो।
उदाहरण: NonNullable with Functions
function findUser(id: number): string | undefined {
return id === 1 ? "Ravi" : undefined;
}
type FoundUser = NonNullable<ReturnType<typeof findUser>>;
const name: FoundUser = "Ravi";
console.log(name);
NonNullable with Objects
NonNullable उपयोगी है जब API boundary पर object properties या values null या undefined हो सकती हैं, लेकिन internal code को validation के उन cases को already rule out कर देने के बाद एक stricter, always-present type चाहिए।
उदाहरण: NonNullable with Objects
interface ApiResponse {
data: string | null;
}
type ValidatedData = NonNullable<ApiResponse["data"]>;
const data: ValidatedData = "payload";
console.log(data);
NonNullable with Unions
NonNullable कई दूसरे members वाले बड़े unions से null और undefined हटा सकता है, union का हर दूसरा valid type रखते हुए सिर्फ nullable parts strip करते हुए, बाकी union को exactly जैसा था वैसा छोड़ते हुए।
उदाहरण: NonNullable with Unions
type Value = string | number | null | undefined;
type CleanValue = NonNullable<Value>;
const v: CleanValue = 42;
console.log(v);
When to Use NonNullable
NonNullable उपयोग करें जब एक type एक ऐसी value represent करता है जो कहीं और पहले ही null या undefined के लिए check हो चुकी है, तो बाकी code को इसे बार-बार re-check करने की ज़रूरत न रहे।
उदाहरण: When to Use NonNullable
function getLength(value: string | null): number {
const checked: NonNullable<typeof value> = value ?? "";
return checked.length;
}
console.log(getLength("hello"));
NonNullable<string | null>से एक runtime null check जुड़ने की उम्मीद करना, जब यह सिर्फ एक type है।- यह भूल जाना कि
NonNullable0,"", याfalseनहीं हटाता, सिर्फnullऔरundefinedहटाता है। - इसे एक function type पर apply करना और
ReturnTypeभूल जाना, तो function खुद प्रभावित नहीं होता।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- TypeScript Advanced Types
- Mapped Types
- Conditional Types
- Custom Type Guards
- Assertion Functions
- Control Flow Analysis
- Exhaustiveness Checking
- Satisfies Operator
- Template Literal Types
- Utility Types - Partial
- Utility Types - Required
- Utility Types - Readonly
- Utility Types - Pick
- Utility Types - Omit
- Utility Types - Record
- Utility Types - Exclude and Extract
- Utility Types - NonNullable
- Utility Types - ReturnType
- Utility Types - Parameters
- TypeScript 5 Updates