Live data from Hacker News

Tricks I wish I knew when I learned TypeScript

cstrnt.dev

21–30 of 276 posts

Re: Tricks I wish I knew when I learned TypeScript

#22

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.

You're absolutely right. Should have stated that I meant a plain object (key-value-pair). But either didn't want to focus on this topic for that post because it would have been just a bit too much to talk about :D

Re: Tricks I wish I knew when I learned TypeScript

#24

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)

or even this

typeName = Object.prototype.toString.call

Re: Tricks I wish I knew when I learned TypeScript

#25
post #6

Note that `Readonly ` does not prevent a call to side-effect methods when `T` is not among a predefined set of built-in types. Indeed, it prevents such calls only on predefined types such as arrays, maps, and sets. It could be more "accurate" to use `readonly number[]` instead of `Readonly >` for highlighting the difference.

That's right. But I wanted to to use `Readonly` because it can also be applied to plain objects while readonly cant

Re: Tricks I wish I knew when I learned TypeScript

#26

I actually don't really like Record types in the way people/library maintainers often use them - the type-checker asserts that values are actually present for all the specified keys, which is fine if the objects with the Record type really were exhaustive; but instead I often see them used where the reality of the data is a Partial - some keys are missing. Something about the abstraction causes people to misuse it fr…

You pretty much always have to define your record type with `| undefined` tacked on to the value type parameter. With that, the problem mostly goes away.

Re: Tricks I wish I knew when I learned TypeScript

#27

I haven't used typescript much but it's surprising to me that you can pass a `const` value to as an argument which is not `ReadOnly`. Does `const` not really mean anything?

That’s a JavaScript specific nuance that typescript inherits.

‘const’ declares a variable with an immutable reference, not an immutable value. If you’re referencing a simple literal like a string or a number that’s effectively the same thing but for objects (and arrays under the hood of JavaScript are fancy objects) while the reference to your given object is constant, the properties of that objects are still mutable.

Re: Tricks I wish I knew when I learned TypeScript

#28
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.

https://javascriptweblog.wordpress.com/2011/08/08/fixing-the...

tl;dr: Object.prototype.toString.call, you most likely to also want to exclude null, RegExp, Function, Date, Number, Boolean & String (not string)

Re: Tricks I wish I knew when I learned TypeScript

#29

I haven't used typescript much but it's surprising to me that you can pass a `const` value to as an argument which is not `ReadOnly`. Does `const` not really mean anything?

"const foo = bar" just means that foo can't be reassigned to some other value. It doesn't say anything about bar.

Re: Tricks I wish I knew when I learned TypeScript

#30
post #27

I haven't used typescript much but it's surprising to me that you can pass a `const` value to as an argument which is not `ReadOnly`. Does `const` not really mean anything?

That’s a JavaScript specific nuance that typescript inherits. ‘const’ declares a variable with an immutable reference, not an immutable value. If you’re referencing a simple literal like a string or a number that’s effectively the same thing but for objects (and arrays under the hood of JavaScript are fancy objects) while the reference to your given object is constant, the properties of that objects are still mutable…

Same as Java’s final, isn’t it. You can use final when declaring a reference to an object but that doesn’t make the object itself immutable, just the reference itself.
Post reply on HN