I realized a brand new software program improvement time period from Scott Hanselman: “stringly typed”.
Scott describes “stringly typed” as follows:
[whenever] you’re passing strings round when a greater sort exists.
An software is “stringly typed” when sort info is offered, but you are calling different methods or capabilities based mostly on strings or stringified information. Unsurprisingly, most API calls are “stringly typed” as a result of request and response our bodies are sometimes stringified JSON information.
const response = await fetch("https://instance.org/subscribe", {
technique: "POST",
physique: JSON.stringify({ username: "...", password: "..." }),
});
const person = await response.json();
JavaScript is a weakly typed language with out a lot sort security, so the kind loss wasn’t a giant deal for me up to now. And since most APIs converse JSON, the potential varieties are restricted to strings, numbers, objects, arrays, booleans and null
anyway.
We have all gotten used to this state of affairs, have not we?
Let’s flip issues round for a second:
- what if we thought-about API endpoints to be distant operate calls?
- what if we thought-about API endpoints to be a part of the frontend functions as an alternative of distant interfaces?
The API name above may very well be seen as a subscribe
operate with a username
and password
parameter that returns a person
object.
In case you’re utilizing a strongly typed language like TypeScript, receiving the person
object as any
or unknown
sort is unlucky. You will lose all the kind security and you’ll solely regain it with handbook sort checking. “Stringly typed” varieties in a type-safe setting suck.
After constructing all these SPAs connecting to APIs being maintained by totally different groups, I’ve by no means thought-about it to be an enormous downside, however now that I’ve a reputation for this sample, “stringly typed” interfaces began to trouble me.
I understand that I would like sort security over the community and I do not wish to cope with “stringly typed” apps in any respect. I would like all the categories!
After all, the issue of “stringly typed functions” is not new and there are JS options like tRPC or typed API interfaces like GraphQL on the market. I performed with GraphQL fairly a bit however will discover sort security over the wire now.
Nonetheless, I ponder if I had simply had a TypeScript epiphany and can begin to uncover (and curse) “stringly typed” interfaces in all places, simply because I now have a reputation for it.