Live data from Hacker News

Type-Safe Printf() in TypeScript

typescriptlang.org

21–30 of 82 posts

Re: Type-Safe Printf() in TypeScript

#21
post #17

Earlier quoted context omitted.

“JavaScript’s success is unfair”, sure, whatever. But that’s not the same as “JavaScript is a failure”.

The point is that success sure tastes bitter.

Yes, as expected of a successful language, JavaScript clearly falls into the first category of Stroustroup’s observation: “There are only two kinds of languages: the ones people complain about and the ones nobody uses.”

Re: Type-Safe Printf() in TypeScript

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

> another language

With the property of verifiably correct behavior

> compiler plugin

A number of languages allow it (Haskell being the most prolific example, but also Java, Scala, gcc, many others)

Re: Type-Safe Printf() in TypeScript

#25
post #13

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.

Pretty easy to succeed when it's the only language a browser supports.

Java and ActionScript -- though technically not supported out of the box -- had practically full support.

Re: Type-Safe Printf() in TypeScript

#26
post #7

not sure i understand the utility of this when format strings and string template types already exist. you can also use typescript-eslint/restrict-template-expressions if you find yourself running into problems with that https://typescript-eslint.io/rules/restrict-template-express...

I think this is less about the utility and more about showing off unusual ways to use the TypeScript type system.

Re: Type-Safe Printf() in TypeScript

#27
post #8
post #3

Except missing the pesky runtime implementation. We don't need though, right? As long as the types say it's right.

The static types depicted in typescript are entirely fictitious. Any similarity to runtime types is purely coincidental.

Guardrails won't keep you on the road if you intentionally steer into them at full speed either.

Re: Type-Safe Printf() in TypeScript

#28
post #12
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 wonder if we should have a kind of "hidden type system", where we still take advantage of having a single type system to reason about, but the extra-specific "weird-ish" types can be hidden, almost like private variables, where visibility is literally hidden from the programmer unless obtained from debug modes or errors.

You mean like the C++ auto keyword but everywhere?

Re: Type-Safe Printf() in TypeScript

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

Sounds like comptime from Zig. There are a few others that does something similar, but Zig probably has most mind share right now.

You parse the string and then iterate over the passed arguments and check if everything adds ups. Rather straightforward.

Expressing it in the type system like TS did is impressive, but not simple.

Re: Type-Safe Printf() in TypeScript

#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?
Post reply on HN