by stickyricky · 4y ago · 0 comments
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.
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.
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.
That and a Node/Deno backend. Which is what the OP is talking about.
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.
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.