News for hackers. Stuff that matters.
Re:Tricks I wish I knew when I learned TypeScript by WA on Tuesday October 12, 2021 @05:49AM (#28837893)
Note: please post a better solution.
Re:Tricks I wish I knew when I learned TypeScript by dgb23 on Tuesday October 12, 2021 @06:41AM (#28838155)
I very, very much disagree with the type of suggestions here. Probably because I don't know TS but here it comes anyways...

You don't need a library or check that it isn't an array or anything like that. What you actually want to check is what it _is_.

You absolutely don't care whether that object is an array or a function or what have you. What you care about in that piece of code is that it satisfies what you want to do with it.

In the context of the article you can do an ad-hoc check on the fields that you require in that context.

Aside:

There are places where you want to have a kind of rigidity around the shape of your objects, including homogeneous collections. The JIT might reward you with optimizations in certain cases. But that is optimization, so you are supposed to measure first and only then apply them or have a very clear picture of how your runtime behavior will be.

Re:Tricks I wish I knew when I learned TypeScript by presentation on Tuesday October 12, 2021 @05:59AM (#28837961)
Probably best to just lift one off of a major library like Lodash, they're well tested and efficient (no need to actually use the library, do include the LICENSE somewhere though):

https://github.com/lodash/lodash/blob/master/isObject.js

But depends on your needs, and you can also attach a typeguard to it.

Re:Tricks I wish I knew when I learned TypeScript by chucky on Tuesday October 12, 2021 @11:01AM (#28840595)
Why not use the library? With tree shaking and/or direct imports you will ensure the same bundle size as if you just copied the file, and you don't have to worry about licenses etc. In fact, since other dependencies might depend on lodash you can deduplicate the import and actually save on bundle size.

You'll also get notified of any security issues in your lodash imports if your CI pipeline is setup for doing that kind of thing.

Re:Tricks I wish I knew when I learned TypeScript by presentation on Thursday October 14, 2021 @11:41PM (#28873962)
Mostly if you look at it out of the context of a single function - a lot of projects end up taking a huge number of dependencies, with a lot of overlapping functionality, because you used one function from this one, another function from that one… I’m fine with using libraries when they actually do heavy lifting that’s core to an application, but a single two line function requiring including hundreds of unrelated irrelevant ones? That will impact the coding style of your team and does have security downsides, like needing to trust the library authors and potentially breaking your build because they changed their APIs or deleted a package or whatever. Copying a 2 line function has very clear boundaries to what it can and can’t do, and doesn’t hide the internals of what you’re doing behind the mystique of “an external dependency”.
Re:Tricks I wish I knew when I learned TypeScript by nicoburns on Tuesday October 12, 2021 @06:10AM (#28838021)
If you want to avoid a library then you can do `typeof x === "object" && !Array.isArray(x) && x !== null`. Not ideal, but you can of course turn it into a utility function.
Re:Tricks I wish I knew when I learned TypeScript by Fckd on Tuesday October 12, 2021 @08:02AM (#28838688)
x && !Array.isArray(x) && typeof x === 'object'.

its typical to start off with `var &&` to avoid operations on null & undefined values

Re:Tricks I wish I knew when I learned TypeScript by Fckd on Tuesday October 12, 2021 @08:28AM (#28838849)
For environment lacks Array.isArray method i think this works well

!!(x && x.__proto__ === [].__proto__)

Re:Tricks I wish I knew when I learned TypeScript by otrahuevada on Tuesday October 12, 2021 @07:50AM (#28838608)
a simple solution would be

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

Re:Tricks I wish I knew when I learned TypeScript by jwalton on Tuesday October 12, 2021 @08:07AM (#28838719)
This isn't a great solution:

    entry = Object.create(null);
    entry.name = "Jason";
    entry.age = 42;
    entry.constructor === Object // false - constructor is undefined.
`entry` is a valid Human here, but fails your check. Actually, just creating a new class that implements the Human interface will cause a similar problem, since the constructor will be the class instead of Object. You don't even really want to exclude arrays here:

    entry = []
    entry['name'] = "Jason";
    entry['age'] = 42;
Again, entry is a valid Human here.
Re:Tricks I wish I knew when I learned TypeScript by otrahuevada on Tuesday October 12, 2021 @08:13AM (#28838760)
I'm having some trouble finding your proposal in the thread... how would you do it?
Re:Tricks I wish I knew when I learned TypeScript by jwalton on Tuesday October 12, 2021 @10:37AM (#28840223)
:) Fair enough:

    function isHuman(obj: unknown): obj is Human {
      return !!obj && 
        typeof obj === 'object' &&
        typeof (obj as any).name === 'string' &&
        typeof (obj as any).age === 'number';
    }
This checks the shape of the object, and returns true if it's a `Human`. This will work for array or objects or classes or anything.
Re:Tricks I wish I knew when I learned TypeScript by nsonha on Tuesday October 12, 2021 @06:51AM (#28838214)
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)