Live data from Hacker News

Tricks I wish I knew when I learned TypeScript

cstrnt.dev

31–40 of 276 posts

Re: Tricks I wish I knew when I learned TypeScript

#32

Note: don't use typeof x === 'object' to check whether something is a valid object, because it will return true for arrays as well. Arrays are objects, so this is expected behaviour.

How about any => Object.prototype.toString.call(any).slice(8, -1)

I really like this package, because it not only uses the method you mentioned for checking types, but is written in TS, so you get the type checking feedback.

https://github.com/sindresorhus/is

Re: Tricks I wish I knew when I learned TypeScript

#34

Nice article, I like it. I can recommend "Programming TypeScript" by Boris Cherny (O'Reilly, 2019) and "Effective TypeScript" by Dan Vanderkam (O'Reilly, 2019), if you want to learn more like this.

And https://exploringjs.com/tackling-ts/

Re: Tricks I wish I knew when I learned TypeScript

#35
post #13

The third example describes something useful in record types, but goes about it in what seems an odd way, and ends up suboptimal as a result. I'd instead use an object type like this: type Human = { name: string; age: number; } which also enforces value types in the compiler, rather than requiring runtime guards.

Which is exactly what was used in the code exemplifying something else in the second example - so that confused me (never having written any TS) too.

Yeah, typically you want a record type for something like a mapping over potentially arbitrary keys (via an index type) to values of known type, and an object type (which can have optional keys) when you do know exactly what shape you expect and want to enforce it.

Re: Tricks I wish I knew when I learned TypeScript

#36
post #32

Earlier quoted context omitted.

How about any => Object.prototype.toString.call(any).slice(8, -1)

I really like this package, because it not only uses the method you mentioned for checking types, but is written in TS, so you get the type checking feedback. https://github.com/sindresorhus/is

[deleted]

Re: Tricks I wish I knew when I learned TypeScript

#37
post #8

Here's another. Instead of returning Sometype|undefined from a function which may or may not have a value to return (such as searchCustomer), return Sometype|null. That forces the function to return a value that's explicitly intended rather than defaulting from a missed out if-else codepath. This is useful since JS is often imperative style code.

I prefer to use undefined over null since it just fits more naturally with other TS features like optional fields. But I agree with using tsc’s no implicit returns check.

Re: Tricks I wish I knew when I learned TypeScript

#39

Is there ever a reason to use interface over type? From what I’ve seen it looks like they can both do the same thing but with slight differences in syntax

Yes interfaces can be merged: https://www.typescriptlang.org/docs/handbook/declaration-mer..., which can be useful when working with external libraries.

Re: Tricks I wish I knew when I learned TypeScript

#40
post #7

Note: don't use typeof x === 'object' to check whether something is a valid object, because it will return true for arrays as well. Arrays are objects, so this is expected behaviour.

Note: please post a better solution.

a simple solution would be

> if(entry && entry.constructor === Object){}

Post reply on HN