Live data from Hacker News

Type-checked non-empty strings

exploring-better-ways.bellroy.com

1–10 of 51 posts

Re: Type-checked non-empty strings

#3

Language is not mentioned in a title, so my first thought was about TypeScript type wizardry. Turns out it's as simple as `Exclude `. https://www.typescriptlang.org/docs/handbook/utility-types.h... Edit: nevermind, LLM fooled me.

It is very much mentioned in the article title and the first sentence. It's just HN that's truncated the title.

Re: Type-checked non-empty strings

#4

Language is not mentioned in a title, so my first thought was about TypeScript type wizardry. Turns out it's as simple as `Exclude `. https://www.typescriptlang.org/docs/handbook/utility-types.h... Edit: nevermind, LLM fooled me.

It's simple, and it doesn't work as `Exclude` only applies to union types. For type `string` it just returns the same type `string`.

Re: Type-checked non-empty strings

#8
Very cool that the language allows specification of a type in this way.

I added a similar type — “non-empty-string” to a typechecker for PHP, and it’s been adopted widely in the PHP ecosystem. It turns out to be pretty handy, especially when there’s a full type system to support it.

Re: Type-checked non-empty strings

#9

Language is not mentioned in a title, so my first thought was about TypeScript type wizardry. Turns out it's as simple as `Exclude `. https://www.typescriptlang.org/docs/handbook/utility-types.h... Edit: nevermind, LLM fooled me.

Speaking of TS, there's stuff in there for typing strings / string formats: https://www.typescriptlang.org/docs/handbook/2/template-lite...

Re: Type-checked non-empty strings

#10

Language is not mentioned in a title, so my first thought was about TypeScript type wizardry. Turns out it's as simple as `Exclude `. https://www.typescriptlang.org/docs/handbook/utility-types.h... Edit: nevermind, LLM fooled me.

It's simple, and it doesn't work as `Exclude` only applies to union types. For type `string` it just returns the same type `string`.

yup, it's not possible to do it safely with a simple unparameterised type: https://www.typescriptlang.org/play/?#code/C4TwDgpgBAcg9gOwK...
Post reply on HN