Hacker News new | ask | show | jobs
by stickyricky 1612 days ago
Whats the purpose of sharing types? Define entities on each and leave behavior as a domain consideration. Clients should be disposable and decoupled. If you want to render html on the server that's fine but svelte isn't the best choice for that.
3 comments

> Whats the purpose of sharing types?

Server sends data as JSON to the client. Client parses JSON. Both now have the exact same data shape. Shared TS types let you use the same representation for the same data no matter where.

At the expense of coupling which is not a trivial concern. Regardless, the browser is the only platform capable of using those types (even so the types aren't shared but generated). You don't use those types in Swift or Java or Kotlin. You don't use those types in a python client library.
In my experience this coupling cannot be avoided. At a bare minimum the client has to know the communication protocol of the server. This means you typically need to define this protocol in both the server and client. If you could define it once and have both the client and server pull from the single source, it's a win.
The coupling is to the data interchange format. The server is not coupled to the client and the client is not coupled to the server.
Why does adding types result in any more coupling than what exists already?

If your client and server both treat field "temperature" as a string for protocol version 1, and then the client upgrades to protocol version 2 and temperature is now a float, the server has to be modified anyway, type system or no type system, because otherwise it'll break.

If anything, the type system helps to expose the fact that the client and server now disagree about the type of a field, which is helpful.

> Why does adding types result in any more coupling than what exists already?

Because the client and server now share a code base. The client requires the server to run.

> If your client and server both treat field "temperature" as a string for protocol version 1, and then the client upgrades to protocol version 2 and temperature is now a float, the server has to be modified anyway, type system or no type system, because otherwise it'll break.

If the data interchange format changes then yes both the client and server need to change. But if either the client or the server wants to coerce temperature to a different type they are free to do so. As long as the conform to the interchange format the internal representation of the data is irrelevant to third-parties.

In a world where types are shared, updates to the server necessitate updates to the client even if the client doesn't want to change (and vice-versa).

> If anything, the type system helps to expose the fact that the client and server now disagree about the type of a field, which is helpful.

The coupling should receive the credit not the type system. Regardless this undoubtedly true. Two isolated pieces of code don't know what the other is doing. You need to test and you need to write design docs.

Much of the point of TS is to make make this kind of refactoring easier, and it doesn't prevent coercing the data format later at all.
> Because the client and server now share a code base.

This isn't true. Types aren't "a code base", they're an interface - without which it is impossible for a client and server to talk with each other anyway.

> The client requires the server to run.

Not relevant. Nothing about a typed interface prevents the client from running without having a running server - it won't be very useful without something to exchange data with, but you can still run it, unless you've coded otherwise.

> But if either the client or the server wants to coerce temperature to a different type they are free to do so.

No, type coercions are bad engineering. They're brittle and only work in a very tiny number of situations - nothing approaching the general case.

> As long as the conform to the interchange format the internal representation of the data is irrelevant to third-parties. Type coercions at the interface level are a hack that is not acceptable for anything beyond hobbyist work.

An interchange format is a set of types. Types are not internal representation - they're a mathematical concept used in type-checking. Types happen to be used in the process of generating an internal representation of data, but that's not what they are. You're falsely equating the two, and your point is invalid without that equivalence.

> In a world where types are shared, updates to the server necessitate updates to the client even if the client doesn't want to change (and vice-versa).

This is false. If the server is changed, the client only needs to change if the interface between the two also changes. If the server makes a change to its internal data storage format, then that has nothing to do with the client, and so the interface stays the same, and so the shared type declarations/schema stay the same.

If the interface changes, then you already have to update both client and server, so the tiny overhead of maintaining an explicit type schema dwarfed by the amount of effort you spent changing the rest of the code anyway, and more than made up for by the compile-time detection of warnings when you change the interface code for the server, compile, and then get an error that the client no longer compiles.

> The coupling should receive the credit not the type system.

A type system will detect type errors at compile-time. A tightly-coupled system with type coercion and no types will detect errors at run-time, if at all. It's obviously better to detect errors at compile-time. Therefore, even if coupling is correlated with the usefulness of a type system, type systems are still obviously beneficial to include.

Regardless, it doesn't make any sense to talk about "credit" in this case. I think that you misunderstand the purpose (and utility) of type systems, but I'm not sure what to suggest to you so that you can understand.

> I think that you misunderstand the purpose (and utility) of type systems, but I'm not sure what to suggest to you so that you can understand.

I enjoy writing code in typed languages. I am in full agreement that they have purpose and utility.

> No, type coercions are bad engineering.

The opposite is true. Your job as a software engineer is to coerce types. I don't mean calling `float64(unsafeString)`. That's certainly type coercion but, as you said, its brittle and probably not the best design.

Consider a database. It stores its data on disk as something (ignore implementation). It loads that data into memory where it is coerced to a type (probably in the C-language). Its filtered, grouped, whatever else. Serialized to something and sent over the wire. Your Python server receives that something and coerces it to a Python type. That Python type is serialized to something and sent over the wire. Your Javascript front-end loads the data and represents it as a Javascript type.

Nothing about this process is bad or ugly or brittle. It would be infinitely worse to maintain the something type across all these different boundaries. Imagine pickling types whenever we want to save something to disk or share it over the network. Imagine being forced to write application code in the same language as your database!

> Type coercions at the interface level are a hack that is not acceptable for anything beyond hobbyist work.

Representing a value in the database as an enum and as its string name in the interchange format is not hobbyist work. I'm certain if you have worked professionally you've used enums and not marshaled their underlying value.

> If the server is changed, the client only needs to change if the interface between the two also changes.

You are advocating for type coercion. The server and client store internal representations and must marshal an interface type to communicate. That's a form of type coercion (I should say "casting" since its explicit but I've mistakenly used coercion so far so we'll roll with it).

---

I think you're too tunnel visioned on the types argument. Types are great. I love them. But coupling your database to your server to your client just to share types between the three is obviously not a good idea. There are greater concerns at play. Extend the argument all the way. Separate your client from your server.

> Regardless, the browser is the only platform capable of using those types

That and a Node/Deno backend. Which is what the OP is talking about.

What expense?
Sharing the types of API returns is very useful.
> Clients should be disposable and decoupled.

No, not really. Clients are loosely coupled, but they must comply with a contract.

And guess what: contracts define types, and REST focuses almost exclusively on types.

> Clients are loosely coupled

Under the architecture I describe, no, clients and servers do not couple to one another. The client and server couple to a data interchange format.

> contracts define types

Yes, the interchange format defines the types for the interchange. But a string can be represented as an enumeration or a float or an integer or a boolean. The type of the data is dependent on context.

> REST focuses almost exclusively on types

In a sense yes, but it also focuses on decoupling clients and servers. The types are really more of an implementation detail. The type of interchange can be XML, HTML, JSON, bytes, text, etc. The fields in a JSON document can all be strings for the purposes of interchange but may be parsed to integers and floats for the purposes of rendering the data to the user. The server may parse those fields differently.

Its not relevant to the client or the server how the other chooses to interpret the data. All that matters is that the interchange format is maintained.