LooseAutocompleteB: T | Omit<U, T> & U

Creates a type that allows for autocomplete suggestions on a custom type (can only be string, number, or symbol), while not giving errors for other values.

Type Parameters

  • U extends string | number | symbol

    A union type that can contain string, number, and symbol, this will be the base type, anything not assignable to this WILL throw an error.

  • T extends U

    A union type of string literals and number literals to add to the autocomplete, string literals are only allowed if U contains string, and number literals are only allowed if U contains number.

// Will allow autocomplete for "abc", "b", and "def", and will not throw errors for other string values.
type Original = LooseAutocompleteB<string, "abc" | "b" | "def">; // "abc" | "b" | "def" | (Omit<string, "abc" | "b" | "def"> & string)

// Will allow autocomplete for 1, 2, and 3, and will not throw errors for other number values.
type Original = LooseAutocompleteB<number, 1 | 2 | 3>; // 1 | 2 | 3 | (Omit<number, 1 | 2 | 3> & number)

// Will allow autocomplete for 1, 2, and 3, and will not throw errors for other number or string values.
type Original = LooseAutocompleteB<number | string, 1 | 2 | 3>; // 1 | 2 | 3 | (Omit<number | string, 1 | 2 | 3> & (number | string))

// Will allow autocomplete for "a", 45, and "fhsd", and will not throw errors for other number, symbol, or string values.
type Original = LooseAutocompleteB<string | number | symbol, "a" | 45 | "fhsd">; // "a" | 45 | "fhsd" | (Omit<string | number | symbol, "a" | 45 | "fhsd"> & (string | number | symbol))