News for hackers. Stuff that matters.
Re:Svelte and TypeScript by orange8 on Wednesday July 22, 2020 @06:11AM (#23915512)
> You can't avoid null and undefined

We are speaking about just null, not "null and undefined". Believe it or not, I simply do not use null in JS. Study the concept of "truthy and falsy" in JS.

> Accessing a missing property or method results with undefined. Hope you remember the exact method names and field names of all objects in your codebase!

I don't. I do however structure my code predictably, using consistent naming conventions and patterns. I hope you do realize JS existed for over 20 years before TS, and complex apps like gmaps and google docx were built entirely in JS.

> Using "str".match(regex) results with null if there are no matches.

const match = /bar/.exec("str");

if( match ){

  // am only interested in what happens if foo returns a *truthy* result.
 
  // The exact type of a falsy result is irrelevant, it either matches (truthy) or it doesn't (falsey)

}
Re:Svelte and TypeScript by spion on Thursday July 23, 2020 @06:26PM (#23933300)
If you forget to do the check, you will get a falsy value. Hope you remember to do it every single time, and hope all your team members remember!

If a team member writes

  function extractEmail(text) {
    return x.match(/someregex/);
  }
and you call extractEmail(x), you might get a null value.

If you assume you can't get a null value, you might try to do further processing:

  let [name, host] = extractEmail(x).split('@')
and get a type error thrown. That is, if you're lucky.

In the more common situation, you will actually save that to a database and that database will happen to accept null values by default, and then all hell will break loose in another totally distant bit of the code that naively fetches a list of emails from the table, grouped by host name. And you will have to chase down the bit of code that inserted that null in.

This is approximately what development without types looks like. When this happens to you, I hope you remember the following bit of advice: It doesn't have to be this way :) Alternatively you can start doing paranoid validation everywhere. That works too, except the nice thing about a type system is that it can properly track whether you need to do that or not (see https://github.com/spion/branded-types for one TS technique on how to do that)

Re:Svelte and TypeScript by schwartzworld on Wednesday July 22, 2020 @12:02PM (#23918422)
> We are speaking about just null, not "null and undefined". Believe it or not, I simply do not use null in JS. Study the concept of "truthy and falsy" in JS.

I'm anti-TS, but there is a meaningful difference between null and undefined. Most of the time it's enough to check for falsy values, but there are times when you will want a distinction between a missing value and a value of null.

https://i.stack.imgur.com/T9M2J.png

Re:Svelte and TypeScript by orange8 on Wednesday July 22, 2020 @11:16PM (#23923852)
Do you have a concrete example of such a time?
Re:Svelte and TypeScript by schwartzworld on Thursday July 23, 2020 @10:49AM (#23927903)
Undefined represents the absence of information altogether. In JavaScript any key can be called on any object and if it doesn't exist it will return undefined. If you put a typo in the key name, you get undefined. If you've never set it, you get undefined.

Null, on the other hand, is not a lack of information. It's an absolute value just as much so as any number or string or array.

They're both falsey values, but so is the number zero. An item with a price of zero dollars is not the same as an item with an undefined price, nor is it the same as an item that explicitly has no price, i.e. null. You may not choose to model your data that way, but the terms are still meaningful, and your UI might have to change to reflect the different falsey values.

Most common case I could think of, would be in making a request from a server, using something like Apollo. `const [ data ] = useQuery(SomeQuery)` The data that comes from the server is undefined until you make the request, but null could be a valid response.

Re:Svelte and TypeScript by Vinnl on Wednesday July 22, 2020 @06:31AM (#23915610)
> We are speaking about just null, not "null and undefined".

Maybe you are, but when people are talking about using TS just for the strict null checking, they typically refer to the (default-on) `strictNullChecks` config option, that is about both null and undefined.

Re:Svelte and TypeScript by orange8 on Wednesday July 22, 2020 @06:45AM (#23915667)
My point is I never use null or worry about "strict null checking" or even what that is in the first place or even how it is configured.
Re:Svelte and TypeScript by spion on Thursday July 23, 2020 @06:36PM (#23933450)
Next time you get a "Cannot read property <x> of undefined" error at runtime, take a moment to trace why it happened, then read https://shinesolutions.com/2017/01/06/writing-safer-code-wit...
Re:Svelte and TypeScript by Vinnl on Wednesday July 22, 2020 @02:38PM (#23920170)
Well, good for you, bit you can run into the exact same problems with undefined (which you do use), and TypeScript still helps with those in the exact same way.

Which is to say: whether you use null or undefined is not really relevant to the point that was being made...

Re:Svelte and TypeScript by orange8 on Wednesday July 22, 2020 @04:15PM (#23921109)
Thank you. I use undefined very rarely, usually with a hacky looking: "typeof foo === 'undefined'". I'd still rather just type that though than having to setup up TS. Don't get me wrong, TS is great for the eco-system, for examples it powers the intellisense behind vscode which I use daily. I just do not need to use it directly, so good for me indeed.
Re:Svelte and TypeScript by bobisme on Wednesday July 22, 2020 @10:07AM (#23917129)
> complex apps like gmaps and Google docs were written entirely in JS

I always assumed complex Google apps from that era were built with GWT, which would mean Java.

Re:Svelte and TypeScript by Zacru on Wednesday July 22, 2020 @10:38AM (#23917484)
No, but they were written using the Closure Compiler type annotations. So closer to TS than plain JS.
Re:Svelte and TypeScript by orange8 on Wednesday July 22, 2020 @04:59PM (#23921513)
Closure compiler was released in 2009, google maps in 2005. So no, you've got it all wrong. The only thing limiting you from using the full power of JS is your mindset. And Google maps is just one example, others include nodejs, electron, react, vue, svelte....
Re:Svelte and TypeScript by mekster on Wednesday July 22, 2020 @08:28AM (#23916208)
null and undefined are very different. Not sure how you leave null out of your code.

You never interact with database? You're not supposed to pass undefined as NULL value.

And a variable that is waiting to be defined is very different from a value that received null as a result.

Dealing all those mixed up as falsy is a bad design.

Re:Svelte and TypeScript by orange8 on Wednesday July 22, 2020 @04:18PM (#23921134)
I've already mentioned this above, please look up falsy and truthy values in JS. Null, false, 0, undefined and "" can be treated pretty much the same in 99% of your JS code. JS is after-all still a dynamic language, and there is nothing stopping you from making use of its dynamic features.