Live data from Hacker News

Type-Safe Printf() in TypeScript

typescriptlang.org

61–70 of 82 posts

Re: Type-Safe Printf() in TypeScript

#61
post #6

Reminds me of Idris: https://gist.github.com/chrisdone/672efcd784528b7d0b7e17ad9c... 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 forma…

Maybe check out Clojure spec?

https://clojure.org/guides/spec

Re: Type-Safe Printf() in TypeScript

#62
post #54

Earlier quoted context omitted.

As a general rule, if something is on the HN homepage and you find yourself asking "am I missing something?", the answer is almost by definition "yes" :) It's just a cool use of some of typescript's more advanced features that many developers probably don't use on a day-to-day basis (likely for good reason, as other comments have pointed out!)

I really enjoy how people try to dispel their outright attempts at bullying behavior with an emoticon. :) Meanwhile, the HN homepage is not some carefully guarded display of exceptional merit, and no serious "hacker" would take the things posted here to be above reproach.

I think it was an attempt to add a cheeky or comical tone to the response, instead of outright saying "Yes, you're missing something" or the more curt "Yes". But if I helps,

Yes, you're missing something.

Re: Type-Safe Printf() in TypeScript

#63
post #20

Earlier quoted context omitted.

Yeah. I see a lot of "typescript is more readable" arguments out there, but I find this code dense and verbose. The more words you use to explain something the more likely you are to be misunderstood. What we're looking at here is basically a restricted wrapper for console.log and a regex implementation meant to simulate a logger in another language. Why not just write a cross-compiler for that language? There's no l…

Hmm, let me try to defend TypeScript, then. I think it’s a terrific language, and more importantly manages to salvage JavaScript into a very decent language. Coming from a mostly C/C++ background, I had been very skeptical of “gradually typed” languages like Dart (and now Python), but I’ve come around to the view that for many purposes it’s better than a completely statically typed compiled language. You don’t need t…

> Hmm, let me try to defend TypeScript, then. I think it’s a terrific language, and more importantly manages to salvage JavaScript into a very decent language

Counterpoint: It's a desperate attempt to make Javascript useable and nearly does so, but ends up being weird in itself to get around the limitations of the underlying language.

I use Typescript most days and I hate it. Part of that is that npm is a dumpster fire but a lot of it is that Typescript is a rubbish version of much better languages and it hurts to use it. I am so desperate to get back to something sane like C# that I will quit this job for less money.

Re: Type-Safe Printf() in TypeScript

#64
post #30

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?

I can't find it now, but someone actually built that for SQL in Typescript as an experiment. The problem folks run into is IDE and compiler performance. These sorts of features are what make your system turing complete, so they start stressing the compiler pretty quickly

Re: Type-Safe Printf() in TypeScript

#66
post #6

Reminds me of Idris: https://gist.github.com/chrisdone/672efcd784528b7d0b7e17ad9c... 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 forma…

I don’t see why static assertions wouldn’t be enough in this case.

Re: Type-Safe Printf() in TypeScript

#68
post #60

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. Please don't adopt this.

printf is about 700 lines in musl libc https://git.musl-libc.org/cgit/musl/tree/src/stdio/vfprintf....

and there's no language-level type safety, although plenty of tools lint printf now

Re: Type-Safe Printf() in TypeScript

#69
post #38

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. 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 s…

While I certainly agree, I've found that this is often an indication of too-complex an architecture, and a fundamental re-think being necessary. I've had projects that depend on [fp-ts], which end up incredibly generic-heavy, but still make it entirely through a typecheck(not build- typescript's just worse at that than other tools like esbuild) in seconds-at-worse. Obviously depends on your organization/project/appli…

How large in lines of typescript are the projects you've used fp-ts or similar with?

We have about 3 million; when I discuss a slow type, i mean a type that contributes ~1 min of checking or more across all the uses in 3 million lines, analyzed from a build profile using Perfetto. I've looked at a generic-heavy library that's similar (?) to fp-ts, effect-ts (https://effect.website/), but I worry that the overhead - both at compile time with the complex types, and at runtime with the highly abstracted control flow that v8 doesn't seem to like - would be a large net negative for our codebase.

Re: Type-Safe Printf() in TypeScript

#70
post #48

Earlier quoted context omitted.

Typescript typing isn't even required, so your argument just fell apart.

I’ve used both and Typescript is clearly better. The optionally has had zero negative impact on my projects. So basically, I simply disagree.

hope you can remember to use the typing EVERY single time. good luck!
Post reply on HN