[Typescript] Tips: Create autocomplete helper which allows for arbitrary values
Ever wanted just a bit of autocomplete? Here, we create a TypeScript helper called LooseAutocomplete which gives us autocomplete while also allowing arbitrary values.
We want IconSize
to be "sm" | "xs"
or any other string value, but we cannot do like this:
type IconSize = "sm" | "xs" | string
This will be IconSize just be string
. And we lost autocomplete.
The way we can do:
type IconSize = LooseAutoComplete<"sm" | "xs">
type LooseAutoComplete<T extends string> = T | Omit<string, T>