Live data from Hacker News

Learn how to unleash the full potential of the type system of TypeScript

type-level-typescript.com

211–220 of 256 posts

Re: Learn how to unleash the full potential of the type system of TypeScript

#211

Earlier quoted context omitted.

It's very simple to understand. You don't have to actually read the definition just know what "compose" does at a high level.

What compose() does is not the point here, it's that the type definition is totally unreadable. I was trying to figure out what compose was supposed to return (the function or the value), in that case, and I still don't really know. Another random example from Axios: (onFulfilled?: (value: V) => T | Promise , onRejected?: (error: any) => any): number; Or eslint: type Prepend = ((_: Addend, ..._1: Tuple) => any) exten…

Single letter variables are bad when they're values, and bad when they're types.

Say no to single-letter variables!

Re: Learn how to unleash the full potential of the type system of TypeScript

#212

Earlier quoted context omitted.

Hard disagree. For me, the proof is in the pudding. When writing regular Javascript, I need to run and debug my code often as I'm developing it to make sure all intermediate states make sense and that there's no edge cases I've missed. With Typescript I can often write code for hours without even starting it up, and when I finally do run it, it usually works correctly right out of the box.

I don't know why it took so long to really take off. I've been using TypeScript since almost day one for these same reasons. Maybe it's because I came from C++. Like in C++, I like the flexibility of not being too under-typed (impossible to not break anything) or over-typed (impossible to do anything).

There are a depressingly large number of people who just don't get static typing. I think a lot of them use very basic editor setups - think Vim or Notepad++ so they don't see half of the benefits.

Re: Learn how to unleash the full potential of the type system of TypeScript

#213

Earlier quoted context omitted.

For sure. I am no TypeScript expert, and for the most part I don't really need to be... most of our types are pretty straightforward, arrays of strings and such. Learning is great, but this job, like many others, doesn't really have a well-defined system for training, documentation, professional development, or anything like that. Either I learn on the job as it happens or I don't learn at all. There's always too muc…

I think I see where you come from. And for libraries and even frameworks I would agree. But typescripts advanced features are more like advanced SQL. Sure, you'll learn some chunk specific to your database, but the majority will be transferable. And just like SQL it won't become outdated knowledge in the next decades most likely. So I still think it will be worth. If not for the company then at least for yourself. :)

That's a good point. Like even if TypeScript goes away, some other language will still use generics and higher order functions. I can see the value in that!

Re: Learn how to unleash the full potential of the type system of TypeScript

#214
post #120

Earlier quoted context omitted.

I’m inclined to say that any magic applied here is a win. Making things complicated with Typescript is simply hard enough that those people that’d mess things up in the first place wouldn’t even consider trying.

Yes, let's write everything in assembly language. Only the real pros will dare touch that! /s Complex to understand code does not entail high quality of maintainers. Quite the opposite in my experience.

That was not my point.

Writing simple types with Typescript is simple, and improves any Javascript code.

Writing complex types (inference, generics, inheritance) is really punishing. And people that don’t truly know what they’re doing won’t even try (or at least in my experience, I’ve never seen them try).

Re: Learn how to unleash the full potential of the type system of TypeScript

#215

Earlier quoted context omitted.

It's very simple to understand. You don't have to actually read the definition just know what "compose" does at a high level.

What compose() does is not the point here, it's that the type definition is totally unreadable. I was trying to figure out what compose was supposed to return (the function or the value), in that case, and I still don't really know. Another random example from Axios: (onFulfilled?: (value: V) => T | Promise , onRejected?: (error: any) => any): number; Or eslint: type Prepend = ((_: Addend, ..._1: Tuple) => any) exten…

That compose thing likely came from a library not application code.

It also must have documentation other than the type annotation.

Re: Learn how to unleash the full potential of the type system of TypeScript

#216

> Over the years, the type system of TypeScript has grown from basic type annotations to a large and complex programming language. Give someone (particularly a developer) the opportunity to build something complicated and undoubtedly they will. So now you have two problems, the complicated program that actually does some hopefully useful work, and another complicated program on top of it that fills your head and slow…

Complexity or not, it's incredibly useful. To me the development experience is just superior, you feel more in control. No one likes building the app only to see "Cannot read properties of undefined" and then needlessly scratching head what's wrong. I have done a large refactor recently and it was such a breeze, with TS pointing out pretty much everything that needs to be fixed to complete it. Coding new features and sometimes it just works after the first build. TS is obviously the inevitable future since it offloads the stuff that the computer can do better and frees up the mental energy for the more creative stuff. The code is also more readable, when the types mean something there's less need for describing function parameters for instance, since you have a complete type definition of what that param represents and any comments made on those types are re-usable across functions.

Re: Learn how to unleash the full potential of the type system of TypeScript

#217

Earlier quoted context omitted.

> And I don't know how to express the correct solution (i.e. where we actually assert that A and B are object types). You can do this: function merge , B extends Record >(a: A, b: B): A & B { return { ...a, ...b } } const result = merge({ a: 1 }, { b: 2 })

This fails on: const result = merge({ a: 1 }, { a: "fdsfsd" }) The correct type is quite complex and depends on whether or not `exactOptionalPropertyTypes` is enabled. EDIT: I think this is correct for when `exactOptionalPropertyTypes` is off. type OptionalKeys = { [K in keyof T]: {} extends Pick ? K : never }[keyof T] function merge (a: A, b: B): { [K in Exclude ]: B[K] } & { [K in Exclude ]: A[K] } & { [K in keyof…

Wow yeah it gets way too complex if you want to track the types of properties within the objects too! If that is the case, then I would just prefer to do this instead as it is much simpler:

  type Value = { a: string }
  const result = merge({ a: 1 }, { a: "fdsfsd" })

Re: Learn how to unleash the full potential of the type system of TypeScript

#218

Earlier quoted context omitted.

I think I see where you come from. And for libraries and even frameworks I would agree. But typescripts advanced features are more like advanced SQL. Sure, you'll learn some chunk specific to your database, but the majority will be transferable. And just like SQL it won't become outdated knowledge in the next decades most likely. So I still think it will be worth. If not for the company then at least for yourself. :)

That's a good point. Like even if TypeScript goes away, some other language will still use generics and higher order functions. I can see the value in that!

My 2 cents: I think the fact that you're having trouble with the compose signature is something you should rectify and that would transfer well to other languages. I'm however not sure the same holds true for more advanced features of typescript.

I'm only a casual user, so take this with more than a grain of salt, but for me typescript occupies quite a weird point in statically typed language space: on the other hand, typescript's type system is enormously complex (and also quite expressive). Part, but I don't think all of this, comes from being retrofitted onto a untyped language and its ecosystem (so e.g. sum-types tend to be implicit rather than tagged, and in addition to discriminated unsions, there is support for complex sub-typing from the OO heritage). Most statically typed languages have no direct equivalent for many of typescript's more advanced features (e.g. partial types, although Scala and Ocaml have related constructs, in Ocaml's case e.g. polymorphic variants).

But on the other hand it's surprisingly awkward to get what I would consider one of the most basic and beneficial features of a sane statically typed language, namely exhaustiveness checks -- so most people don't even bother. In fact there is not even an agreed upon idiom (just google "exhaustiveness check typescript", all the answers will look spuriously different). The basic pattern is that you want a helper function like so:

   function assertUnreachable(_value: never): never {
     throw new Error("Statement should be unreachable");
   }
and then for any switch(foo) you do an default: assertUnreachable(foo). I can't really fathom why there isn't a better way to express this (the ability is clearly there, but it's un-ergonomic). But if you want something that transfers well, I'd probably de-emphasize the fancy stuff typescript offers unless needed for acceptable JS interop and concentrate more on thinking about exhaustiveness and making undesirable states unrepresentable.

Re: Learn how to unleash the full potential of the type system of TypeScript

#219
> Expect>

This seems like a great time to bring up "Why Static Languages Suffer From Complexity"[1], which explains the "statics-dynamics biformity" that leads to languages like TypeScript that are actually two languages: the runtime one and the compile-time type-system one.

[1] https://hirrolot.github.io/posts/why-static-languages-suffer...

Re: Learn how to unleash the full potential of the type system of TypeScript

#220

Earlier quoted context omitted.

A simple trick with plain JavaScript is to give all arguments default values. That gives a pretty good insight for anybody reading the code as to what "type" of arguments the function expects. If you test-call such a function without arguments you will then know what kinds of values you can expect it to return. The argument default values can not be inner functions but they can be any function that is in scope. Or if…

> That gives a pretty good insight for anybody reading the code as to what "type" of arguments the function expects. If you test Only true if you're using very simple types, i.e. number and string. But "string" is pretty close to "any" and doesn't give you much info. If my function only expects two or three possible strings, it should be typed to only take those ones. Comments are not a solution for much of anything,…

> Comments are not a solution for much of anything,

What are Unix man-pages but comments about the APIs they describe? Are you saying you would prefer to replace them with the TypeScript type-language?

As I see it TypeScript is a a solution to the problem of how to describe a function, what it does, what it expects from its arguments and what it returns. That information can often be clearly and simply expressed with a comment, rather than with a complicated type-declaration. And type-language only describes the syntactic behavior of a module, not its semantics.

When type-declarations become more complicated than the code they are describing I think we're at a point of diminishing returns.

The other purpose of type-declarations is to catch errors. But if a declaration is very complicated how can we be sure there's no errors in it?

Post reply on HN