3 ms·
Language is not mentioned in a title, so my first thought was about TypeScript type wizardry. Turns out it's as simple as `Exclude<string, "">`. https://www.ty
by ivanjermakov 3mo ago
Language is not mentioned in a title, so my first thought was about TypeScript type wizardry. Turns out it's as simple as `Exclude<string, "">`.
https://www.typescriptlang.org/docs/handbook/utility-types.html#excludeuniontype-excludedmembers https://www.typescriptlang.org/docs/handbook/utility-types.h...
Edit: nevermind, LLM fooled me.
- ralferoo 3mo agoIt is very much mentioned in the article title and the first sentence. It's just HN that's truncated the title.
- antipurist 3mo agoIt's simple, and it doesn't work as `Exclude` only applies to union types. For type `string` it just returns the same type `string`.
- phpnode 3mo agoyup, it's not possible to do it safely with a simple unparameterised type: https://www.typescriptlang.org/play/?#code/C4TwDgpgBAcg9gOwKIFsygMrAE4EsEDmAPACpQQAewECAJgM5T074EB8UAvEy4eVTQZQyAfigIIANwjYoALmH9qdRgCJVUMROmyFJANwBYAFAmAZgFcEAY2C5EUAIb16M4PGRpMvYmUrKhZjxCNgAKfDALYAUPVHQQLGDfNgBKBWdXbGBGCKioXEZYrwSfUg4AbxMTAF8q4wy3IvjE1lDVOABrVRSTBqym7yS27t6XRsQ4wdb1J0Yg1h7TY2tEZigzODgFeb5udTq+9wnilsJQjbhFkxWENYAjR1k91QOx-uPmn1CH7Cvl1eAUAAjhYKFwoO0uq9MkdPJ8hiCKH8gA https://www.typescriptlang.org/play/?#code/C4TwDgpgBAcg9gOwK...
- Cthulhu_ 3mo agoSpeaking of TS, there's stuff in there for typing strings / string formats: https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html https://www.typescriptlang.org/docs/handbook/2/template-lite...
- nvme0n1p1 3mo agoDaily reminder that TypeScript's type checker is not sound. https://www.typescriptlang.org/play/?#code/C4TwDgpgBAcg9gOwKIFsygMrAE4EsEDmUAvFEgB4DGANgK4AmEAPAM474EA0UA5DwHwBuAFDDKiNlDhwwLErx4jxCSQkSp0IAFyx1aTO0LzpspRLjUIAOmpwCACjXJ9IGxELAAFgEpBQA https://www.typescriptlang.org/play/?#code/C4TwDgpgBAcg9gOwK...
- IceDane 3mo agoThis example is not only wrong for what you intend to demonstrate but even if it wasn't, it's not problematic. In typescript the proper way to do this is using branded types and exporting only the safe constructor, making anyone who wants to violate the invariant go out of their way, which is no different from the situation in any number of programming languages or scenarios. declare const brand: unique symbol; type NonEmptyString = string & { readonly [brand]: 'NonEmptyString' }; // the ONLY non-cast way to produce one export function nonEmptyString(s: string): NonEmptyString | undefined { return s.length > 0 ? (s as NonEmptyString) : undefined; } export type { NonEmptyString };
- blue_pants 3mo agoIt is true, but it wasn't meant to be sound, so it's okay. You can do this trick for type-checking emptiness of string literals https://www.typescriptlang.org/play/?jsx=0#code/C4TwDgpgBAsgrgZ2AOQPbAEIQKIFsygA8AKlBAB7AQB2AJglEgE4CW1A5gHxQC8jwrDmUo16UYgFgAUFCgB+KNQgA3CE2myAXOOFU6DAOQH5ilWqjbiAbmnSAxqmpJFqPARABlAW3a8oJXVEGZh9OAApmbXgkNEwcfCJiTgBKXm5mGylbKQcnYCgmCAQARj9qVwTPbw4wgwAzVFQDZMyAelbZToA9OWzc50KEACYyivcvQXZa5raOzqgevsdncuoAGRYqJgBDABsJnz8wsNSebiNkk8z+-MGAZlG3UAOa1Y2tvZf2Ful2+YXelIgA https://www.typescriptlang.org/play/?jsx=0#code/C4TwDgpgBAsg...
- recursive 3mo agoDaily reminder that the unsoundness in Typescript's type checker is a practical compromise applied to a language that was never designed to be type checked in this way, and furthermore that many developers feel that it strikes a good balance between formal correctness and practical usability.