Open roryabraham opened 3 months ago
Could be useful, but someone has to prove that it works in practice without too many downsides.
Some alternatives:
type NonEmptyString = string & {0: string}
type NonEmptyString = string & {length: Exclude<number, 0>};
type NonEmptyString = string extends '' ? never : string;
It seems pretty common that we run into issues with a string being empty when it's not supposed to. These are some common issues I see:
'-1'
, but our ESLint config encourages us to use nullish coalescing??
instead of||
. Obviously these don't work the same in cases where you have an empty string.anyways, my suggestion is to create a type called
NonEmptyString
. According to this Stackoverflow, the best way to accomplish this is with a very verbose template literal with any non-empty character followed by a stringUpvote & Fund