“What’s a union?”
“Okay… then what’s a discriminated union?”
“Okay… let’s apply destructuring to one…”
“Okay… now what would analysis look like there given what I know?”
“Okay… how do we have the computer know what I know intuitively as a human given this context?”
“Okay! I think I got it!”
:-). If you’ve gotten this far with programming to understand the examples above, my guess is if you just take the time you can do it without the examples if you take it in small enough chunks :-).
Life’s all about what you apply your energy too
(And also, wonderful example :-) )
https://github.com/sindresorhus/type-fest/blob/main/source/r...
I can walk through it with enough pause, but it kinda makes my head implode when I glance at it.
{ [Key in KeysType]-?: ... }
TIL that you can disable ? and readonly in mapped types using -. So in the above code, every Key will be mandatory (-?) even if the original one in KeysType was optional.source: https://www.typescriptlang.org/docs/handbook/2/mapped-types....
I guess in a way, the issue is that it is cloaked with familiarity. Your brain gets so used to understanding sentences on first go it’s not used to not understanding it.
However, if it was a chemistry equation and you weren’t familiar with chemistry it would immediately understand that comprehension would take time and thus the mind implosion due to a lack of understanding doesn’t occur.
type HttpVerb = "GET" | "POST" | "PUT" | "DELETE";
But you can also do more complex stuff: type User = {name: string; password: strings} | OAuthID;
By themselves they aren't hard to understand or use.As an aside, compared to sum types, you don't have to define them before using them: a function can return number | null while in language with sum types you usually define option first and then return option<number>.
In a language like TypeScript, they can be used for function overloading since there is no runtime dispatch based on argument type. But usually they're used as sum types in the same way as e.g. Rust enums.
Typescript supports so-called "singleton types", in which a given type is inhabited by exactly one value. This lets you do things like specify not just `string` but specific values like `"foo" | "bar"` as a type. In these cases, I often use them in combination with Typescript's "mapped types", which let you index into a map with a certain type and get another type out. This can be useful for things like RPC calls, where the type of the output depends on the type of the input.
Ah, this answers my question I guess. Thanks for the info. I'm about to start a new job doing Typescript stuff so I am going to be deep diving into the language to get my head around it, so this helps a lot :-)
A record is a set of key-value pairs with the value having a given type (also called a struct, object, etc. depending on the context). In TypeScript, this is an example of a record:
Destructuring is an ergonomic feature where you can take the fields of a record and assign them to local variables. For example: Besides ergonomics, destructuring can offer features like exhaustiveness-checking, to make sure that you used all of the fields of a record.A union type is a type which denotes that the value is one of two different types, indicated like this:
Sometimes, you have two different cases with the same type, and you want to distinguish between them. For example: To handle this case, you can use a discriminated union instead. These unions have a special value which unambiguously indicates which case is present: Sometimes, you have a discriminated union with different fields: Currently, TypeScript uses control-flow analysis to ensure that you're using the right kinds of fields depending on the discriminator value: However, you can't combine this feature with destructuring: With this pull request, you can do the above. This can be convenient with patterns like reducers (see https://github.com/microsoft/TypeScript/issues/46143 for an example).