Live data from Hacker News

Tricks I wish I knew when I learned TypeScript

cstrnt.dev

251–260 of 276 posts

Re: Tricks I wish I knew when I learned TypeScript

#251
post #178

Earlier quoted context omitted.

I'd love to see an explanation of monads that accurately captures their capabilities in terms no more complex than those required to do the same for promises.

I had a pop at this here https://dev.to/choc13/grokking-monads-in-f-3j7f

Great write up!

Re: Tricks I wish I knew when I learned TypeScript

#253

Utility Types[0] will help you get to the next level on Typescript. It's important to know them and know how and when to use them. [0] https://www.typescriptlang.org/docs/handbook/utility-types.h...

I really don't get the point of operations on types such as 'type TodoInfo = Omit'. Is this a real use case? Why not just literally write down the properties? Surely easier to read and in the end maybe even easier to maintain.

Re: Tricks I wish I knew when I learned TypeScript

#254

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…

So you didn’t read the (very easy to read) documentation and then complain that you can’t figure out how to use it? And then you wonder why people are downvoting you?

Re: Tricks I wish I knew when I learned TypeScript

#255

Earlier quoted context omitted.

exactly.. the 'only' billion dollar mistake in JS in regards to null is that typeof null === 'object' => true

It’s not undefined, therefore it’s an object. And because every type can be null, it makes sense that it’s just “object”, not “string” or whatever.

typeof null should be "null", just how typeof undefined is "undefined"

it is an implementation bug of netscape, null was represented by the zero pointer and objects where tagged pointer with a tag of zero, so when reading its tag null looked like an object.

there are solutions as x == null, x === null, and Object(x) == x allow you to check for null|undefined, null, and object values, but typeof null being "object" is purely a specification bug that it is too late to change.

Re: Tricks I wish I knew when I learned TypeScript

#256

Earlier quoted context omitted.

exactly.. the 'only' billion dollar mistake in JS in regards to null is that typeof null === 'object' => true

It’s not undefined, therefore it’s an object. And because every type can be null, it makes sense that it’s just “object”, not “string” or whatever.

It it just a mistake or oversight in the definition of the language. It doesn't really make logical sense that a null is an object but a string is not an object.

It would kind of make sense in Java, where only object types can be null. But that distinction does not exist in JavaScript.

Re: Tricks I wish I knew when I learned TypeScript

#257
Short comment about the 'unknown' type: TypeScript 4.4 had me learning about it very recently, because 'unknown' has been made the default type in 'catch (error)' clauses [0]. So most of our code in 'catch' blocks suddenly didn't compile any more after an unsuspecting update of the TS version.

Which is a good thing, because in reviewing those I found several places where incorrect assumptions were being made about the type of error that would be caught.

(side note: TypeScript does not follow SemVer; they just promise to avoid breaking changes in Patch updates, but don't promise anything won't break in Minor updates [1])

[0]: https://devblogs.microsoft.com/typescript/announcing-typescr...

[1]: https://github.com/microsoft/TypeScript/issues/14116#issueco...

Re: Tricks I wish I knew when I learned TypeScript

#258

Earlier quoted context omitted.

The advantage here is that you are not repeating the type of the property twice (once as optional in the base type, once as mandatory in the extended class). Even though the code may seem inscrutable, note that the resulting type is fairly easy to understand in your IDE. That is, if you hover over the "B" to see what the type definition is, you see: type B = A & { foo: number; baz: number; } If you defined the type l…

The beginner programmer copies the property definition. The advanced programmer simply writes "type Ensure = T & { [U in keyof Pick ]-?: T[U] };", thus removing the need to copy the property. The master programmer copies the property definition.

Consider it in the context of a framework like React. The type of setState() is defined as Partial. The framework cannot just copy paste the type definition with properties set to optional, since the state type is defined by the user and specific for each component. Without type helpers there would be no way for a framework to define the type for setState().

I'm not sure how often you would need type helpers in application code, for frameworks and libraries they are a godsend.

Your argument is a bit like saying we don't need parameterized types like Array because you can just copy paste the code for each type.

Re: Tricks I wish I knew when I learned TypeScript

#259
post #117

Still working on this, but might give someone a laugh :) Problem 1 from Project Euler in TypeScripts Types https://github.com/iiTzEddyGG/typing-euler/blob/master/src/p...

I am highly interested in type programming lately, exactly the kind of thing you're doing here. Do you have any other resources that inspired you? Here are some of my favorites: https://gist.github.com/hediet/63f4844acf5ac330804801084f87a... https://github.com/codemix/ts-sql https://github.com/jamiebuilds/json-parser-in-typescript-ver... https://gist.github.com/acutmore/9d2ce837f019608f26ff54e0b1c...

One of my favorites is Josh Goldberg's implementation of Tic-Tac-Toe in the type system:

https://blog.joshuakgoldberg.com/type-system-game-engines/

Re: Tricks I wish I knew when I learned TypeScript

#260

Earlier quoted context omitted.

Array.isArray[0] is your friend [0]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Did anybody else have their brain do a weird backflip seeing `Array.isArray[0]`?? Object.getOwnPropertyDescriptors(Array.isArray) /* { '0': { value: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray', writable: true, enumerable: true, configurable: true }, length: { value: 1, writable: false, enumerable: false, configurable: true }, name: { value: 'isArray', writable: fal…

How did you get that result? I only see length and name.
Post reply on HN