r/webdev • • May 18 '25

Discussion I wonder why some devs hate server side javascript

I personally love it. Using javascript on both the server and client sides is a great opportunity IMO. From what I’ve seen, express or fastify is enough for many projects. But some developers call server side javascript a "tragedy." Why is that?

193 Upvotes

253 comments sorted by

View all comments

Show parent comment

27

u/LakeInTheSky May 19 '25

Thank you for mentioning the lack of types at runtime. This is very important and people don't talk much about it.

I've been working as a front-end dev for many years now, but my first languages were C++ and Java. When TypeScript appeared, I liked it, but I knew it could never fully solve the lack of type checking for JavaScript.

3

u/thekwoka May 19 '25

provided you are using typescript native deps and not using any and as all over the place, it's solves most of them at least for the typesafety aspects.

But it is structurally typed, and not nominally typed like many fan favorite typed languages are. But that's not that often a real concern.

4

u/c-digs May 19 '25

That's not really the problem: the problem is backend APIs need to accept inputs from the external world to do something interesting. Those could be API boundaries, reading from storage (S3, GCS, Azure Blob Storage, file system, etc.), reading from a database (Firebase, Postgres jsonb, etc.)

In languages like Java and C#, a fast serializer will make it impossible to assign a string to an int property. In JS, this is not the case. The native JSON.parse doesn't care so there is a need for runtime schemas and runtime type validators like Zod, Valibot, class-validator, ArkType, etc. Whereas in C#, for example, the out of the box System.Text.Json is fast, type safe, can be used to generate natively compiled serializers, and flexible using annotations on domain types to control serialization.

My point here is that if you care about type safety in your backend -- which is why a team might adopt a Zod or class-validator -- then just use a language and runtime which has runtime types because that solves a whole class of problems and does not bog down performance trying to emulate types at runtime via schema artifacts.

1

u/thekwoka May 19 '25

In JS, this is not the case. The native JSON.parse doesn't care so there is a need for runtime schemas and runtime type validators like Zod, Valibot, class-validator, ArkType, etc.

You don't need to use JSON.parse, you can use other things.

n languages like Java and C#, a fast serializer will make it impossible to assign a string to an int property

Sure, by doing runtime parsing and validation.

You can emulate the same kind of thing to have your statically analyzable type system prevent the same kinds of issues.

then just use a language and runtime which has runtime types

JavaScript is Dynamically typed, it has runtime types.

The amount of people in this thread that don't really understand what type systems are is astounding.

Like, you can choose in Java to just parse JSON representation of an object into a dynamic map, and now you have none of that type protection.

But you don't.

You can similarly have rules and usage of TypeScript that prevents the arbitrary treatment of the result of JSON.parse.

Now, sure, the one is nicer and generally better, but the way you're describing it is just so crazy wrong.

3

u/ub3rh4x0rz May 19 '25

It's really just a caution to statically typed language noobs (they will all blow up at runtime if casts result in runtime looking different than compile time) and those who plan to mix typescript with vanilla js.

5

u/c-digs May 19 '25 edited May 19 '25

It's not about casts, it's about data ingress to your backend. Your backend is not doing anything interesting if it has no data ingress. For data ingress, you're going to need to load things from a database, from a file, accept inputs from a web API.

So you need to verify that those things match the shapes that you expect at runtime because the runtime has no types. A TypeScript signature:

updateName(id: string, name: string) {
   // Update a field on a document in a document DB
}

Will accept updateName('1u7k', { payload: { name: 'Ada' }}) at runtime and...do something depending on the logic. If this is writing to a document DB, then you can see how this would be problematic.

So to prevent this type of issue, runtime type schemas are necessary to preserve the type information that was lost in the compilation from TS to JS. At dev time, this is fine. At runtime where the inputs are coming from externally facing APIs or a file or a database record, you need runtime schemas.

In Java, Kotlin, and C#, for example, you do not need runtime schemas because the types maintian their metadata through the compilation process. So it is impossible in a C# or Java to pass an object into a method parameter that expects a string.