3 ms·
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 proba
by chrismorgan 6y ago
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.
- phpnode 6y agoit's a bit fragile only due to my laziness, it could be done properly if this were a serious project.
- 3np 6y agoHow would it be done if done properly?
- phpnode 6y agosame 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.
- nicoburns 6y ago> 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)
- ianhorn 6y agoStringly 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 6y agoI think they implemented a generic nested language framework (whose real name eludes me).
- dcre 6y agoThe rationale for template literal types is discussed in the recent blog post about the TS 4.1 Beta. https://devblogs.microsoft.com/typescript/announcing-typescript-4-1-beta/#template-literal-types https://devblogs.microsoft.com/typescript/announcing-typescr...