Earlier quoted context omitted.
Wow, thanks for this. I wasn't even aware of these. I'm surprised I rarely see these in courses/tutorial. These should be like day 1 material.
Typescript is actually a great language. And with those utility types, you can do pretty fun stuff like, for example, you want to mutate a type so that some fields become mandatory: type Ensure = T & { [U in keyof Pick ]-?: T[U] }; class A { foo?: number; bar?: number; baz?: number; } type MandatoryFields = "foo" | "baz"; type B = Ensure ; const b: B = { foo: 42 }; Here, ts will complain that b is missing baz.
Tricks I wish I knew when I learned TypeScript
121–130 of 276 posts
Re: Tricks I wish I knew when I learned TypeScript
#122I never knew the depth of type systems until the last year when I took a type theory course and a compiler course taught by a man who loved his types. So much power - I can't wait to see the future of type systems.
Re: Tricks I wish I knew when I learned TypeScript
#123Earlier quoted context omitted.
That example disappointed me a little. It was an easy catch because I was told there's an issue, but I'm surprised const arrays don't at least have a warning there. Or even default to having readonly-like behavior
Which "const" do you mean? The one in front of a variable declaration cannot make the array itself constant. That's because that "const" only refers to that variable itself, which is just a pointer (except for the primitive types). The variable declaration "const" means this variable cannot be changed to point to a different object. It says nothing about the thing it points to and that is how that keyword was designe…
Surely they could have gone with, like, `final` or `immutable` instead..? I'm sure they had their reasons. But seems rough.
Re: Tricks I wish I knew when I learned TypeScript
#124Earlier quoted context omitted.
type Ensure I define a type called Ensure This type takes two type parameters, one called T and the other K which will consist of Keys belonging to the type T (in our case, "foo", "bar" or "baz"). = T & This new type (called Ensure) will be equal to the union of two types: One will be T and the other will be: { [U in keyof Pick ] A new type which keys will be picked among the key listed in K -? To which we will remov…
This feels like considerable cognitive load for any developer that needs to work in more than one language.
Re: Tricks I wish I knew when I learned TypeScript
#125Earlier quoted context omitted.
This seems extremely hard to read for me, I would have an hard time trying to understand what it does if I found it in any source code
type Ensure I define a type called Ensure This type takes two type parameters, one called T and the other K which will consist of Keys belonging to the type T (in our case, "foo", "bar" or "baz"). = T & This new type (called Ensure) will be equal to the union of two types: One will be T and the other will be: { [U in keyof Pick ] A new type which keys will be picked among the key listed in K -? To which we will remov…
You mean intersection, right?
EDIT: link to docs on intersection types: https://www.typescriptlang.org/docs/handbook/2/objects.html#...
Re: Tricks I wish I knew when I learned TypeScript
#126Earlier quoted context omitted.
This feels like considerable cognitive load for any developer that needs to work in more than one language.
Isn't that the main challenge of being a programmer? Anyways I don't find it hard to read, but I write TS like that every day
Re: Tricks I wish I knew when I learned TypeScript
#127Edit: So I take it from the downvotes that everyone else is a genius and I'm the only one that has had a problem learning TS.
Re: Tricks I wish I knew when I learned TypeScript
#128https://www.typescriptlang.org/docs/handbook/2/indexed-acces...
Re: Tricks I wish I knew when I learned TypeScript
#129I'm going to be one of those people, but multiple "=" rendering as a large double line is really ugly.
You don't have to suffer (subjectively) bad typography. Cascading style sheets were designed with the ability to override author styles with user styles, your Web browser has settings for this.
Re: Tricks I wish I knew when I learned TypeScript
#130Does Typescript get better? I've been forced to use it for my most recent project, but haven't been given any time to read through the documentation. I've found that I'm spending about 99.9999% of my development time trying to figure out how to get my IDE to not show that their are typescript problem. At this point I hate typescript with the burning fury of a trillion suns. I wonder if this is everyone else's experie…