Live data from Hacker News

Type-Safe Printf() in TypeScript

typescriptlang.org

51–60 of 82 posts

Re: Type-Safe Printf() in TypeScript

#52
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 think, from having it used recently, that supabase's TS library does this. I had to write a wrapper around it a few months ago at $dayjob and was really surprised when select/from parts of a "query" (not really a SQL query, because it's just a postgrest query) actually got parsed at compile time and spit out the right types. And since our code is pretty type heavy, I was gonna have to do that anyway, so I really appreciated it

Re: Type-Safe Printf() in TypeScript

#53
post #50

Earlier quoted context omitted.

Go's type system is inferior to Typescript's in every possible way. About the only saving grace it has is that it didn't start out in a non-typesafe ecosystem, but most of those issues have been alleviated in Typescript for years. At this point, folks are seeing how far they can push the Typescript type system because it's capable of expressing so, so much more with its types than is seen typically(including in most…

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

You’ll convert, it’s only a matter of time.

Re: Type-Safe Printf() in TypeScript

#54

Am I missing something? This is just a toy implementation of a function prototype, that only includes integers and strings?

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!)

Re: Type-Safe Printf() in TypeScript

#55
post #16

Earlier quoted context omitted.

> If JavaScript was a failure By almost any reasonable measure it is one of the strongest candidates for “biggest success ever as a programming language”. Sure, you can argue that the reasons for that aren’t mainly language design related, but it is absolutely not anything like a failure.

It only succeeded because it literally couldn’t fail. It had plenty of competitors and all of them had one critical flaw: not being built in to the browser.

Being multi-paradigm with a Java like syntax doesn’t hurt.

Re: Type-Safe Printf() in TypeScript

#56
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?

There is an implementation of SQL that operates on a table shaped type, entirely at type level. For your amusement: https://github.com/codemix/ts-sql

There are a bunch of more practical takes that codegen types from your database and generate types for your queries, eg: https://github.com/adelsz/pgtyped

To me the second approach seems much more pragmatic because you don’t need to run a SQL parser in your typechecker interpreter on every build

Re: Type-Safe Printf() in TypeScript

#57
post #48

Earlier quoted context omitted.

But the Go type system is pathetic in comparison to Typescript.

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.

Re: Type-Safe Printf() in TypeScript

#58
post #54

Am I missing something? This is just a toy implementation of a function prototype, that only includes integers and strings?

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.

Re: Type-Safe Printf() in TypeScript

#59
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…

The nature of ts also means that if you make your files slower to build via type/list nonsense, the language server is going to bog down and the editing experience will mysteriously become bad for everyone. Strongly discourage doing slow stuff with the type system.
Post reply on HN