Word of warning: the typescript compiler is not a particularly fast evaluator of recursive list manipulation programs, which is what these kinds of types are.<p>They’re great in small doses where you really need them, but overuse or widespread use of complex types will make your build slower. It’s much better to avoid generics or mapped types if you can. The typings for a tagged template literal (without digit format specifiers like %d4) don’t require any generics.<p>I love to write code like this, but I’m guilty of over using fancy types and I flinch when I see a typescript build profile showing 45s+ spent on generic types I wrote without realizing the cost.
Minor nit: I’ve found types like these—that is, iterative recursive types—benefit from using terminology common to map/reduce. And by “benefit from”, I mean become more understandable by a wider audience—not necessarily the HN audience per se, but quite likely teammates and future selves.<p>Which is to say, these names almost always make types like this more clear:<p>- Head: the first item in the input type you’re iterating through<p>- Tail: the remaining items or unprocessed structure you’ll likely recurse on next<p>- Acc (or pick your favorite “reduced” idiom): a named type for the intermediate product which will become the final type when you finish iterating. This can be provided as an optional parameter with an empty tuple as its default, largely modeling a typical reduce (apart from inverting the common parameter order).<p>It also helps, IME, to put a “base case” first in the type’s conditions.<p>When all of these names and patterns are utilized, the resulting type tends to look quite a lot like an equivalent runtime function you could encounter for producing the value equivalent to its type. This is great because you can even write the runtime function to match the type’s logic. This demonstrates both what the type is doing for people who find these “complex types” intimidating, and that the type accurately describes the value it’s associated with.
Cool.<p>There is a way to make this easier to extend, though: <a href="https://tsplay.dev/WGbEXm" rel="nofollow">https://tsplay.dev/WGbEXm</a><p>Can't tell off the top of my head if there are any disadvantages to this approach though.
Neat, but this is basically a ripoff of this post from a few years ago (even to the point of not including the runtime implementation):<p><a href="https://www.hacklewayne.com/a-truly-strongly-typed-printf-in-typescript" rel="nofollow">https://www.hacklewayne.com/a-truly-strongly-typed-printf-in...</a>
Reminds me of Idris: <a href="https://gist.github.com/chrisdone/672efcd784528b7d0b7e17ad9c115292" rel="nofollow">https://gist.github.com/chrisdone/672efcd784528b7d0b7e17ad9c...</a><p>Recently though, I've been wondering whether advanced type system stuff is the right approach. It usually becomes pretty complicated, like another language on top of the regular language. Maybe it would be easier to have some kind of framework for compiler plugins that do extra checks. Something that would make it easy to check format strings or enforce rules on custom attributes, like Linux's sparse does, using plain imperative code that's readable to the average dev. Large projects would have an extra directory for compile time checks in addition to the tests directory they have now.<p>But I haven't seen any language community do something like that. What am I missing?
The interesting thing here is that the typesafe printf has its function arguments inferred from a string literal, at compile time. You can change the 9 to a "9" and see the type error even before running the code.<p>This is something that most mainstream language's type system cannot do.<p>(This may be obvious, but a lot of commenters here might have missed that.)
not sure i understand the utility of this when format strings and string template types already exist.<p>you can also use <i>typescript-eslint/restrict-template-expressions</i> if you find yourself running into problems with that<p><a href="https://typescript-eslint.io/rules/restrict-template-expressions/" rel="nofollow">https://typescript-eslint.io/rules/restrict-template-express...</a>
I've been kind of curious why tricks like this aren't used more to make sql and such. Heck, you could do similar tricks for shell execution. Or any general "string that is parseable." Seems we always take the route of not parsing the string as much as we can?
Honestly, if you spend that much code on a single `printf`, I will reject your PR and we will have a conversation about code maintenance and cost.<p>Please don't adopt this.