Reputation: 488
To clarify: This is nominally about Immutable.js' Map/OrderedMap, though the essence of the question is general to any TypeScript API (/structurally typed language).
I think I understand technically why in effect Map<K,V> extends OrderedMap<K,V>
holds, despite it never being written in the source code (only the other way round):
TypeScript's structural typing dictates that, as objects of types Map and OrderedMap have the exact same outwardly visible properties/methods (fields), and these fields have the same (generic) types, Map and OrderedMap are actually considered the same type.
But in practice, there's more to the contracts of these datatypes than the fields and their types. OrderedMap guarantees iteration order being the insertion order, while Map doesn't. This should make OrderedMap a proper subtype of Map - i.e. an OrderedMap object fullfills the contract of a Map object, but not the other way round.
One could could easily make the typechecker "understand" this - and highlight code where an OrderedMap is expected but a Map is provided - by adding something like a "isOrderedMap: true" field to OrderedMap.
Shouldn't this be done? Are there valid drawbacks to this tightening of type compile-time safety?
Upvotes: 2
Views: 73