4 ms·
This got me thinking: I regularly read source. And one of my pet peeves with TypeScript is that I will click on a type and have to walk half a dozen types deep
by Waterluvian 5y ago
This got me thinking:
I regularly read source. And one of my pet peeves with TypeScript is that I will click on a type and have to walk half a dozen types deep to understand what it is. Because the library has sensibly abstracted types to be DRY and whatnot.
What I want is a type flattener that I can point at a type and it basically links it all so I see a raw type in all its verbose but complete glory.
- Jach 5y agoI still think this is a great appeal of Clojure(Script), you often work with the "flat" "just data" representations directly. From https://youtu.be/VSdnJDO-xdg?t=3000 https://youtu.be/VSdnJDO-xdg?t=3000 (transcript: https://github.com/matthiasn/talk-transcripts/blob/master/Hickey_Rich/ClojureMadeSimple.md https://github.com/matthiasn/talk-transcripts/blob/master/Hi...): Example - HttpServletRequest getAsyncContext, getAttribute, getAttributeNames, getCharacterEncoding, getContentLength, getContentType, getDispatcherType, getInputStream, getLocalAddr, getLocale, getLocales, getLocalName, getLocalPort, getParameter, getParameterMap, getParameterNames, getParameterValues, getProtocol, getReader, getRealPath, getRemoteAddr, getRemoteHost, getRemotePort, getRequestDispatcher, getScheme, getServerName, getServerPort, getServletContext, isAsyncStarted, isAsyncSupported, isSecure, removeAttribute, setAttribute, setCharacterEncoding, startAsync, startAsync... getHeader, getHeaderNames, getHeaders, getIntHeader... "How many maps?" Vs. In Clojure, Just Use Maps {:remote-addr "127.0.0.1", :scheme :http, :query-params {"somekey" "somevalue"}, :form-params {}, :request-method :get, :query-string "somekey=somevalue", :content-type nil, :uri "/foobaz", :server-name "localhost", :params {"somekey" "somevalue"}, :headers {"accept-encoding" "gzip, deflate", "connection" "close", "user-agent" "Apache-HttpClient/4.1.2 (java 1.5)", "content-length" "0", "host" "localhost:8383"}, :content-length 0, :server-port 8383, :character-encoding nil} "In Clojure, we just use maps. This stuff came over a wire in HTTP, as text. How did we turn it into this [the Java]?"
- Sinidir 5y agoThis will never cease to produce laughter in me :)
- eyelidlessness 5y agoThis is almost exactly TypeScript’s worldview at the type level. It’s just structural types. The only difference is that it’s statically analyzable as a core feature. JS is a little more complicated than that, but (sorry to tell you) so is ClojureScript, where a “map” can become a bunch of different actual data types through the course of a normal runtime. In all cases, sending plain maps/pojos is nice but receiving them is fraught. Again TypeScript helps here by providing type guards where your parse/validation code can be statically analyzed. Edit: and in terms of serialization, all of these languages struggle with fidelity loss. Clojure has EDN or Transit if you’re willing to incur one cost or another. But by default you’re sending out a smaller subset of types than your runtime is using, and receiving the same.
- fulafel 5y agoI'm guessing you mean it's similar to the map case? Clojure/Script of course approaches validation and parsing using schema systems (malli or spec) that has its advantages compared to static type based approaches.
- eyelidlessness 5y agoI mean TypeScript’s structural type system is effectively “just use maps”. With very few exceptions, any structurally equivalent type is substitutable for another. > Clojure/Script of course approaches validation and parsing using schema systems (malli or spec) that has its advantages compared to static type based approaches. So does TypeScript. It has to, because the type system doesn’t have a runtime. The benefit in TS is that you also get static analysis.
- fulafel 5y agoCan you point to some exemplar TS schema system(s) suitable for structured data parsing and validation? I've seen io-ts but it seems quite clunky in comparison, but maybe it's not representative.
- nyanpasu64 5y agoI want a type flattener and function call tree flattener for C++ and Rust, since I use those languages more than JS. Right now I make do by flattening call trees myself in Google Docs, but that's time-consuming.
- noduerme 5y agoI'm a TS dev and only switched in the last month from decades of Eclipse to VS Code. "Peek call hierarchy" is my favorite feature. Granted it doesn't totally flatten an inheritance chain but it saves a ton of time versus going one by one through references.
- eyelidlessness 5y agoYou can use a merge type (on mobile so this may be wrong, but hopefully gets you on your way to where you’re going): type Merge<T> = { [K in keyof T]: T[K] } Typical usage is with intersection types like: Merge<A & B> Where the resulting type is not aliased, at least at the top level, and you just see the structure of both A and B. But it should also work for inheritance, interface extensions, and object type overloads. I think as of either 4.4 or 4.5 you can also safely make it recursive.