116 karma · joined September 11, 2011
let lowerCased = (s : string[] | undefined) => s!.map((str : string) => str.toLowerCase());
console.log(lowerCased(['A','B']));
console.log(lowerCased(undefined)); //boom!
It doesn't coerce anything. All it does is let the code pass without error. "s!.map" and "s.map" compile to the exact same Javascript code, which means there is no type coercion going on. Are you sure you know what type coercion is? The first log prints out fine and the second blows up, which proves that undefined isn't being coerced to a string[]. If that was the case, the resulting JS would be checking for undefined and probably creating an empty array.At least that's what it looks like. I've never used typescript or I'd test it right now. I'm on my phone and haven't tried it.
(Edit: my answer didn't quite make sense! It's different since without ! it compiles as an error because, in this case, you can't map over undefined. With !, it just ignores that possibility although it could still happen. The compiler cannot verify all run time possibilities since your sum type says it really can be undefined.)
(Re edit! Verifying the types vs skipping that verification is not the same thing as coercing the types)
Are you sure about that? It sounds more like a signal to the compiler to "trust me, I know what I'm doing" and would blow up if you passed undefined anyway. Maybe the author didn't explain it very well. That is, not a type coercion so much as a hint to not bother checking this access.
This is not at all unusual. And it's hardly a thing to flame the messenger on. It's just a reality some people deal with.
We had this problem with Ant many years ago. Everyone did their builds differently and half of them were subtly broken.
I have direct experience doing this sort of thing for years. It's really just a strategy for better leveraging your existing business assets. What is the value of saying, "That's no application programming interface, it's just an implementation detail." I think it just confuses the concept for possible surrounding context. If that context is missing (negotiating, somewhat hostile parties), then all you've done is say that you can't call it API. Hmmm ok then...
Or say I offer a REST API at work to some business functionality locked up in a mainframe in order to expose it for reuse by other apps and workflows, and the consumers desperately want me to do it (or anyone who has the time to do so). What is political about that? Talking about the API is going to revolve around requirements that other apps have that could be satisfied by such an API. How is it useful to view that as a type of political border dispute?
API seems to be a fairly narrow concept to me that appears in various contexts, some of them political and others not.
You actually are free to express yourself however you like without violating laws or the civil rights of others. All I heard in the way of a definition is "... and I also want to be free of any consequences I really don't like.". Well, too bad because that doesn't exist and asserting such a "freedom" isn't much different than asserting that unicorns exist. It's a fantasy concocted in your head, not a real thing.