← Back to TypeScript Course | Chapter 8: Advanced Types | Lesson 17 of 20

Utility Types में NonNullable

NonNullable<T> एक type से null और undefined हटाता है। यह उपयोगी है जब एक value के उपयोग से पहले उसका मौजूद होना known होना चाहिए।
Syntax
typescript
type Result = NonNullable<type | null | undefined>;

Basic NonNullable

NonNullable<T> एक type T से null और undefined हटाता है जबकि union के बाकी हर member preserve करता है, एक ऐसे type को narrow करते हुए जो nullable हो सकता था एक ऐसे में जो guaranteed न हो।

उदाहरण: Basic NonNullable

typescript
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

typescript
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

typescript
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

typescript
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

typescript
function getLength(value: string | null): number {
  const checked: NonNullable<typeof value> = value ?? "";
  return checked.length;
}
console.log(getLength("hello"));
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. NonNullable<string | null> से एक runtime null check जुड़ने की उम्मीद करना, जब यह सिर्फ एक type है।
  2. यह भूल जाना कि NonNullable 0, "", या false नहीं हटाता, सिर्फ null और undefined हटाता है।
  3. इसे एक function type पर apply करना और ReturnType भूल जाना, तो function खुद प्रभावित नहीं होता।

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.