Live data from Hacker News

Tricks I wish I knew when I learned TypeScript

cstrnt.dev

121–130 of 276 posts

Re: Tricks I wish I knew when I learned TypeScript

#121

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.

Ok, the comments that this is a little hard to read are fair... but this is really cool. Thanks for sharing.

Re: Tricks I wish I knew when I learned TypeScript

#122
I'm so happy powerful type systems are more popular now. TypeScript bringing a great type system to a language as popular as JS is fantastic. Rust is also a great way to get a great type system while staying in a systems programming and procedural environment. Hell, even python type annotations support union types.

I 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

#123
post #61

Earlier 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…

Wow, the fact that `as const` has such a meaningful difference from a variable declared as `const` seems... not great.

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

#124

Earlier 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.

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

#125
post #90

Earlier 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…

> This new type (called Ensure) will be equal to the union of two types

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

#126

Earlier 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

Probably depends upon your job. The main challenge of being a programmer, in the sorts of projects I work on, is communication - with customers, management, and other developers.

Re: Tricks I wish I knew when I learned TypeScript

#127
Does 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 experience?

Edit: 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

#129
post #97

I'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.

Of course, I'm not going to muck around with brittle webpages that can't take a webpage just to fix one blog post, though.

Re: Tricks I wish I knew when I learned TypeScript

#130

Does 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…

Try fixing the problems
Post reply on HN