3 ms·
What the heck are reverse mapped types?
- lupire 2y agoBrendan Eich's bosses would be so proud that after 30 years JavaScript has finally become a true Java script. What are "reverse mapped types" called in regular computer science type theory?
- ivanjermakov 2y agoSome context on the term: > This is the lingo used in the compiler's source code, in the PRs and in the issues on GitHub. > When you have to infer the T from a MappedType<T>, you are inverting the action of the MappedType. There is a dedicated part in the compiler for doing that. Source: https://www.reddit.com/r/typescript/comments/1elj65m/comment/lgwjwn5 https://www.reddit.com/r/typescript/comments/1elj65m/comment...
- zarzavat 2y agoTypeScript has a very different set of axiomatic operations than conventional type theory. It incorporates concepts from set theory, you have unions T|U, intersections T&U, comprehensions { [T in U]: e(T) }, conditionals (T extends U ? A : B ), pattern matching (infer T), and the typeof operator is like a poor man’s dependent typing. While you could certainly express all of these concepts in a language with dependent types, you can’t emulate the inference rules (the subject of TFA), so it can be said that TypeScript’s type theory is unique.
- lifthrasiir 2y agoAn inverse function. While technically used like types, TypeScript "types" are really compile-time values that happen to be also usable for constraining usual run-time values like types. These compile-time values themselves do not have much types, so you can easily get errors from compile-time evaluations (which is different from compile-time type errors!). Likewise, most TypeScript type-level constructs are really long-winded methods to do the compile-time computation and have no straightforward equivalents in type systems that do guarantee the soundness and coherence (which have specific definitions).
- Kinrany 2y agoSince there's no previous stage where compile-time expressions could be type checked, the best they can have is strong dynamic typing, no?
- lifthrasiir 2y agoNot quite, because the previous stage can be dynamically generated as needed.
- tobyhinloopen 2y agoAlternative title: how to write unmaintainable, unreadable code that nobody understands and cannot be fixed or modified without introducing compiler errors for code that just works. These features are why I don’t like Typescript. It is too complicated.
- BenoitP 2y ago> It is too complicated. I wouldn't say this. It can bring complications, yes: but only because like a lot of expressive tools people will over-engineer things with them (probably like this sentence) To me, Typescript saved JavaScript from under-constraining. It's bringing me some peace of mind.
- ngruhn 2y agoI think using more descriptive type parameter names (instead of just T, K, F, …) already goes half way reducing the complexity.
- mrkeen 2y agoOK so what would you call them? TypeParam1, TypeParam2, ... ?
- code_biologist 2y agoA common convention I've seen is using T at the start of a type parameter, [1] for example. TKey, TData, TError, TSuccessCallback, etc. I always liked T at the end, but I've not seen that as often. [1] https://github.com/TanStack/query/blob/59a6d3df3035d6effeaed884f4575b1a0ef8e788/packages/react-query/src/useQuery.ts#L15-L23 https://github.com/TanStack/query/blob/59a6d3df3035d6effeaed...
- toastedwedge 2y agoSeems apt that the error for a generic type would (and should) be spelled "terror."
- 2y ago
- neonsunset 2y agoThis is why I have been cautious about TypeScript. Quite often it leaves me with impression that it's a language that enables the same kind of type astronaut developer that also hurts Rust. Except in TS this truly has no benefit besides superficial perception of improved type safety. Please, if you have powerful type system, it does not mean it warrants pushing it to its limits, and strikes me as the behavior everyone tends to suffer from in the middle of a learning process of a new tool.
- feoren 2y ago> Except in TS this truly has no benefit besides superficial perception of improved type safety. You know the saying that everyone who drives slower than you is a moron, and everyone who drives faster than you is a lunatic? You seem to be in a mode of "every type system more powerful than what I'm used to is unnecessary", and I'm sure you believe the other half: "every type system weaker than what I'm used to is for morons". Keep in mind, this article is about a detail inside the compiler that makes type inference "just work" seemingly magically. It's not a detail you need to worry about. If you think powerful type inference and mapped types are just for the "perception of improved type safety", you are revealing that you have simply never attempted to learn them. > if you have powerful type system, it does not mean it warrants pushing it to its limits, and strikes me as the behavior everyone tends to suffer from in the middle of a learning process of a new tool. These kinds of features are more for library developers. You should be very glad that the people who make the libraries you rely on every day have access to these kinds of features, because when they do their job well, things "just work" so invisibly that you have no way of appreciating what's actually going on in the background. These "astronaut developers" that you disrespectfully deride are the entire reason you're able to do your job at all. Have a little respect.
- neonsunset 2y agoIf you pay attention to phrasing "hurts Rust", you may notice that this isn't a criticism of TS by itself but rather the misuse of its type system, and the certain set of commonly adopted norms that I personally happened to get presented with when interacting with it in the past, examples in this article being a continuation of the trend. It isn't about expressivity or lack of thereof but unnecessary steps taken to achieve point B from point A.
- nsonha 2y agoThere is no such thing, typescript has features like inferred type params and conditional types. I dont see the point of writing a fancy article just to give them a new name.
- nsonha 2y agoActually I re read the article, not what I thought and this indeed was one thing that puzzled me that turns out to be an undocumented typescript feature.