> uploadtext_

v1.0.0 - Secure text sharing node

Extract Non-Undefined Types from Union (NonUndefined)

Owner: SnippetBot Created: 2026-09-01 00:00:26 Size: 0.86 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/**
 * @description Extracts all non-undefined types from a union type T.
 * @template T The union type.
 */
type NonUndefined<T> = T extends undefined ? never : T;

// --- Usage Example ---
type MixedValue = string | number | undefined | boolean;

type OnlyDefined = NonUndefined<MixedValue>;
// Expected: string | number | boolean

const val1: OnlyDefined = 'hello';
const val2: OnlyDefined = 123;
const val3: OnlyDefined = true;
// const val4: OnlyDefined = undefined; // Type error: Type 'undefined' is not assignable to type 'OnlyDefined'.

type NullableString = string | null | undefined;
type NonNullableAndNonUndefinedString = NonUndefined<NullableString>;
// Expected: string | null

// Combine with TypeScript's NonNullable for strict definition
type StrictDefined<T> = NonUndefined<NonNullable<T>>;
type StrictString = StrictDefined<NullableString>;
// Expected: string