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

1

u/efari_ May 21 '25

you're implying that when making an API in another language, you don't need to check your inputs...?

1

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

I don't have to imply; I can explicitly state: a typed runtime will not allow the incorrect type to be handled and will throw an exception on a type mistmatch because it does not lose type metadata in the build process and it enforces those types at runtime.

record Person(string FirstName, int Age);

async Task UpdatePerson(Person p) {

}

Will fail on this payload:

{ "FirstName": "Ada", "Age": "twenty seven" }

Because "twenty seven" is a string. (In fact, it will fail at serialization automatically without additional effort because the serializer can introspect the type and see that a string cannot fit into an int)

JS will accept this payload and thus it is necessary to create a schema validator using tools like class-validator, Zod, etc. It doesn't matter that at dev, time, TS prevents you from doing this; at runtime, the TS gets erased and it's only JS and JS will happily accept that payload and process it.

The difference is that this type checking is free on typed runtimes like Java and C# by virtue of defining the type and retaining the type metadata at runtime. The same as if you used Typia in TypeScript because Typia inlines the type information at build into JS.

1

u/efari_ May 21 '25

“Included in the price” is not the same as “free” but ok

1

u/c-digs May 21 '25

What's the price? Price implies I've given something up in exchange. Is it slower? Lower throughput? Would love to know what the price is here.

In fact, it is in JS where the price is paid in lower throughput and peformance by requiring Zod or another runtime schema validator to replace the loss of runtime types.

1

u/efari_ May 21 '25

the price is you defining the type and the scheme...

1

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

I did not define a schema; I merely defined a type. In a typed runtime, the build process does not wipe out the type metadata and retains it so it can be introspected.

Let's look at this in TS:

type Person = {
  firstName: string
  age: number
}

const PersonSchema = z.object({
  firstName: z.string(),
  age: z.number()
})

Or alternatively, I give up my type:

const PersonSchema = z.object({
  firstName: z.string(),
  age: z.number()
})

type Person = z.infer<type PersonSchema>

// And now I am working with Zod, not types because my types
// are inferred from Zod; the type-schema relationship is 
// inverted.

Or I have to use class-validator like approach:

class Person {
  @IsString() // 👈 The runtime JS needs this to know this is a string
  firstName: string; // 👈 TypeScript knows this is a string

  @IsNumber() // 👈 The runtime JS needs this to know this is a number
  age: number; // 👈 TypeScript knows this is a number
}

In fact, it is here that I've done extra work, is it not?

I iterate again, this is what I do in C#:

record Person(
  string FirstName, 
  int Age
);

And I'm done because my type metadata does not get wiped out at build time