Last active
September 20, 2024 20:44
-
-
Save alvarogfn/62e1c2a558a57910ff8f99ea4f89eb75 to your computer and use it in GitHub Desktop.
Accept any string but keep auto complete
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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