Lurker News

Comments (191) 32 threads

chrismorgan3 replies#
I’m impressed/fascinated/horrified by the way it parses a string into a type with a variant of template literal syntax—though it looks like the parsing is probably very fragile. I’m curious why that functionality was added to the language.

The end result is actually pretty approachable.

phpnode1 reply#
it's a bit fragile only due to my laziness, it could be done properly if this were a serious project.
3np1 reply#
How would it be done if done properly?
phpnode#
same mechanism, just deal with more edge cases. e.g. whitespace isn't really trimmed correctly at the moment, it doesn't handle escaping quotes in strings and so on. I did think about trying to match postgres's grammar but it's a lot of work and probably implementing a PEG first would make it easier.
nicoburns1 reply#
> I’m curious why that functionality was added to the language.

I think it's because there are a whole bunch of APIs in javascript where there is a function like `.get("foo")` that internally calls a function called `getFoo` or similar. And this functionality allows such functions to be accurately typed. It basically allows strong typing for stringly typed functions (which are pretty common in dynamic languages)

ianhorn1 reply#
Stringly typed functions frustrate me to no end. Tooling for programming gets better every year, yet stringly typed code is so resistant to any of those improvements. Pycharm at some point had to add SQL tools in python strings for gods sake. That kind of weird deep special casing is everywhere with strings drive behavior. Why use a class (inspectable) when a dictionary, a function, and an if statement (opaque) will do?
AlphaSite#
I think they implemented a generic nested language framework (whose real name eludes me).
Tarean1 reply#
The implementation isn't even that horrible. Ignoring the integer list. That workaround is still hillarious to me.

Anyway, it seems both impressive that someone did this and that it actually works. Weird how typescript has the nicest typelevel programming I know, even nicer than some dependent ones. It probably is the only language one where autocompletion properly works with computed types?

phpnode1 reply#
> The implementation isn't even that horrible

You're making me blush!
The list of integers could be replaced by using tuples and reading their lengths to do basic arithmetic. Might run into recursion limits though.

jitl3 replies#
Not really. In the Typescript type system tuple types are array types where the length is encoded in the type system. Tuple types can be “added” by concatenation to produce a new tuple type:

    type two = [0, 0]
    type three = [0, 0, 0]
    type five = [...two, ...three]
    type TupleToNumber = T[“length”]
    const num: TupleToNumber = 5

Continue this thread — 3 replies

alangpierce#
Great project, really expands what feels possible with the new improvements in TS 4.1.

I wonder if this strategy could also be used to have strongly-typed GraphQL queries without the need to have a type generation step that you run separately. Currently when using GraphQL with Apollo, it's great that you get types corresponding to all queries, but having to run the type generator and maintain types in __generated folders is definitely an extra hassle.

Load more comments