Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What's the correct way to use typescript when you're directly working with the Dom, and using minimal jQuery? It seems to really not like it if you're explicitly asking for things you know you have in your document, but it reports an error.


> What's the correct way to use typescript...

You write TypeScript the same way you write JavaScript. It just enforces a lot of the proper discipline and practices that JavaScript doesn't, and it doesn't let you incorrectly use APIs.

> you're explicitly asking for things you know you have in your document, but it reports an error

I'm not 100% sure what you're talking about, but I bet you'd get a great answer on StackOverflow by posting example code and the error message.

First of all, TypeScript doesn't know what's in your document unless you built the document with TypeScript objects. If it's HTML, TypeScript can't read or understand it.

My guess is that you're using something like "getElementBy..." and then TypeScript complains that you're using a value that might be null. You just need to check if the value is null and (for example) throw an error. Once you do that, any line after the null check will assume there's a value.

There are other workarounds when you're 100% sure you don't need TypeScript's error checks (like // @ts-ignore flags), but I strongly recommend against using them.

> using minimal jQuery

Why are you using jQuery at all? It's a solution for a problem that doesn't exist anymore. Just use vanilla JS.


I'm using jquery for certain layout libraries that I need unless you have another library that provides it. Also I'm using datatables. I don't think there's an alternative to datatables that even comes close to being as well put together.

There's just a huge number of libraries around that depend on jquery, and are rock solid with continuing support. I would prefer to use something proven than something that will be gone in a couple of years or abandoned or the underlying library will end up not supporting a feature that widget requires.

I don't want to come across as unappreciative, but there's still reasons to use jquery.


> I don't think there's an alternative to datatables that even comes close to being as well put together.

One of my projects uses ag-Grid[1]. It works very well and doesn't depend on jQuery. There are lots of others to choose from, including FancyGrid[2] and a vanilla-JS version of datatables[3].

ag-Grid has paying enterprise clients that contractually obligate the developers to continue support. As a guarantee of future support, that's as good as it gets in the open-source world.

> rock solid with continuing support

It looks like jQuery itself is not very actively developed anymore[4].

> I don't want to come across as unappreciative, but there's still reasons to use jquery.

It's completely fine to disagree. That's what makes HN interesting. I didn't take it personally.

1. https://www.ag-grid.com/

2. https://fancygrid.com/

3. https://github.com/Mobius1/Vanilla-DataTables

4. https://github.com/jquery/jquery/issues/3886


> something that will be gone in a couple of years

I suspect jQuery and it's ecosystem will themselves become irrelevant and eventually (effectively) abandoned. The (at the time compelling) rationale of abstracting away different browser behaviour has become less and less necessary, and what I would see as the other advantage of using jQuery - like easier access to behaviours like animation, $ajax - less relevant.

$ajax supports async/await, yes - https://petetasker.com/using-async-await-jquerys-ajax/ - but why would I not want to just use the fetch apis? https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API (ignoring browser compatibility, which is becoming the norm)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: