Skip to content

Instantly share code, notes, and snippets.

@alvarogfn
Last active September 20, 2024 20:44
Show Gist options
  • Save alvarogfn/62e1c2a558a57910ff8f99ea4f89eb75 to your computer and use it in GitHub Desktop.
Save alvarogfn/62e1c2a558a57910ff8f99ea4f89eb75 to your computer and use it in GitHub Desktop.
Accept any string but keep auto complete
// Intersect string with {}:
export type StateWords = | 'first' | 'second' | 'third' | string & {}
// Record<never, never> can also work:
export type StateWords = | 'first' | 'second' | 'third' | string & Record<never, never>
// These intersections trick TypeScript into thinking that it is a "different" string type that literal string types are not assignable to (and thus the union won't get reduced to only string).
type StringAutoComplete<T> = T | string & {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment