TypeScript allows incorrect object values to be passed if the key type is disjoint

204 Views Asked by At

I'm using Type Flavoring to keep different types of keys separate in my typescript app. However, I noticed that this has the strange side effect of allowing objects of the form

{[key: SomeFlavoredString]: ObjectA}

to be passed to functions that takes arguments of the form

{[key: SomeOtherFlavoredString]: ObjectB}

without complaining, which can lead to some surprising bugs. Here's a playground

I think what's going on is that it assumes that since the key types are disjoint, the extra keys are irrelevant and so it doesn't matter that the values at those incompatible keys are incompatible values, so it's equivalent to passing an empty object from the typescript compiler's perspective.

Is there some way to disable this or insist that my arguments can't have any incompatible values at any key? This doesn't seem like desirable behavior

0

There are 0 best solutions below