There is also a compromise between documentation through type/readability and type expressiveness. Can you figure out a solution? Meanwhile, it gets you no closer to the goal. It very much is like a puzzle: you’ve got some rules and an objective. It’s similar to the Object-Oriented practice of really puzzling out those isA relationships. Rich Hickey mentioned puzzles as being addictive, implying that it’s fun to do stuff in the type system because it’s like a puzzle. There is a fine line to walk, between using types as a leverage to assist you with your work, or having sophisticated types just for the sake of it, as the clojure community often emphasizes: Resolving those deeply recursive types consumes a lot of time and resources and might in the future run afoul of the recursion governors we have in the checker.īut at this point we’re really deep down the rabbit hole and it’s time to go back to the real world □ (or maybe I should write □). While it may work for small examples, it will scale horribly. It’s clever, but it definitely pushes things well beyond their intended use. This is very impressive, but as Anders Hejlsberg, the Lead architect of Typescript emphasized, don’t do it: iterables : IterableArray ) : Vector ), which you then navigate using a condition it is unable to simplify out right away (here using conditional types: B extends ? 1 : 0). Let’s define the list of settings that our server offers: Each setting is referred to by a key and is of a certain type. Imagine our typescript code runs on the client-side, on a web page, and we need to fetch setting values from the server. Mapped types: fetch settingsĪfter tuple types, let’s now look at mapped types, which were introduced in Typescript 2.1, through another, more concrete example. However now we see that not only it is possible to express the type of function parameters using tuple types, but on top of that, typescript can infer them, and we can reuse this type in other parts of the function signature. Note that we must specify extends any in the generic constraints so that typescript understands we want tuple type inference. And then it returns another function, taking the same parameters T, but returning Option. So lift takes as a input a function taking parameters, the types of which we collect as a tuple type T, and returning U|undefined. So for instance the parameters of this function: Tuple types were supercharged with typescript 3.0, when it became possible to infer the type of parameters of a function as a single tuple type, including optional parameters and all patterns that can be used in function parameters. The type number (array of numbers) is quite different from the type (a tuple with one element which is a number). Typescript has had tuples for a long time, and so is a tuple type, and and are examples of inhabitants of that type. Let’s start by taking advantage of tuple types. Tuple types: prelude-ts: Either.lift, Option.lift, Future.lift Tuples types, an introduction ![]() In general in this post, I’ll first write down the type definitions, and then explain then afterwards, so don’t worry if something is not clear immediately. That’s why we have some dummy implementations like return undefined as any. In the end, implementation is a javascript problem, for this post we’re only interested in the type checking, which is typescript’s domain. In this blog post we won’t be looking at function implementations, only type signatures. ![]() ![]() ![]() We’ll also make an optional excursion into bizarro world, where we’ll abuse typescript’s type system to make it achieve things it was never meant to achieve (and that, in truth, it can only achieve in trivial examples). This post covers a few use-cases for more advanced type constructs in typescript which I’ve met with recently, to illustrate the power of typescript’s type system and give some practical examples to its usefulness. It is easy to treat typescript as a “java” with a couple of bonuses (like or types/union types, keyof and strictNullChecks), but as this post tries to illustrate, that would be leaving on the table a lot of the power offered by the language. Typescript functional-programming types prelude-ts
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |