Live data from Hacker News

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

type-level-typescript.com

31–40 of 256 posts

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

#31
post #24

Speaking of types, what are your thoughts on fp-ts if you've used it? It brings functional programming concepts like in Haskell such as monads into TypeScript.

haven’t used it myself but other teams at the company I work for have tried with mixed results. It’s very opinionated about the way you structure your code and basically makes anything thats not fully fp-ts hard to integrate, and also is quite hard for general JS people to wrap their head around. It’s been designed by FP people for FP people and if there are some on your team who are not fully on board or are just st…

Totally agree. I wrote my thoughts and experiences on FP-TS and maybe those csn help -https://rockyj.in/2022/03/24/fun-with-composition-2

IMHO, functional TS is great with ramda, currying etc. and solves a lot of problems nicely. See also https://mostly-adequate.gitbook.io/mostly-adequate-guide/ch0...

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

#32

A great idea. Now, everyone that learns this stuff, show some restraint! The drawback of a powerful type system is you can very easily get yourself into a type complexity mudhole. Nothing worse than trying to call a method where a simple `Foo` object would do but instead you've defined 60 character definition of `Foo` capabilities in the type system in the method signature. Less is more.

The types in your code are just as designed just like any other aspect of it. It's not a matter of restraint, it's a matter of doing things on the correct way.

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

#34

A great idea. Now, everyone that learns this stuff, show some restraint! The drawback of a powerful type system is you can very easily get yourself into a type complexity mudhole. Nothing worse than trying to call a method where a simple `Foo` object would do but instead you've defined 60 character definition of `Foo` capabilities in the type system in the method signature. Less is more.

Second this. By the end of a progressive multi-year TS migration at my last company, we were refactoring `HTTPRequest>` back into its JS ancestor `HTTPGetRequest`.

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

#35

A great idea. Now, everyone that learns this stuff, show some restraint! The drawback of a powerful type system is you can very easily get yourself into a type complexity mudhole. Nothing worse than trying to call a method where a simple `Foo` object would do but instead you've defined 60 character definition of `Foo` capabilities in the type system in the method signature. Less is more.

The types in your code are just as designed just like any other aspect of it. It's not a matter of restraint, it's a matter of doing things on the correct way.

So less can be more but more can also be more, more or less?

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

#37
One think I struggled a lot with until I got it is that the TypeScript types and the JavaScript code live in totally separate universes, and you cannot cross from the type world to JavaScript values because the types are erased when transpilling - meaning they can't leave any trace.

This means that it's impossible to write this function:

    function isStringType(): boolean { return ... }

    const IS_STRING: boolean = isStringType();
At best you can do something like this, which is inconvenient for more complex cases:

    function isStringType(isString: IsString): boolean { return isString }

    const IS_STRING_1: boolean = isStringType(true); // compiles
    const IS_STRING_2: boolean = isStringType(false); // type error
You basically need to pass the actual result that you want in and just get a type error if you pass in the wrong one. Still better than nothing.

Link if you want to play with it online: https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABDA...

Put another way, you can't do reflection with TypeScript.

You can write that function in C++ templates, and I naively assumed that it's possible in TypeScript too, since from my observations TypeScript allows complex typing to be expressed easier in general than C++.

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

#38
post #25
post #16

I wish this issue would get addressed: https://github.com/microsoft/vscode/issues/94679 Showing fully resolved types in Intellisense would be the single largest usability enhancement they could make for me right now..

Yeah, this is a recurring pain. I know the universe at large has moved away from eclipse, but I loved their rich tooltips where you had nice structured representation (not just a blob of text from lsp) and could click through and navigate the type hierarchy.

Try any IntelliJ IDE, they’ve got that. I can’t understand why anyone would want to use VS Code with a Typescript project voluntarily…

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

#40
post #37

One think I struggled a lot with until I got it is that the TypeScript types and the JavaScript code live in totally separate universes, and you cannot cross from the type world to JavaScript values because the types are erased when transpilling - meaning they can't leave any trace. This means that it's impossible to write this function: function isStringType (): boolean { return ... } const IS_STRING: boolean = isSt…

You can use type guards for something similar: https://www.typescriptlang.org/docs/handbook/advanced-types....
Post reply on HN