Comment by RichieAHB
3 years ago
One thing I’ve never found a name for (and, as a result, a good way to reference to people) is Typescript’s interesting behaviour around the Erased Structural Types mentioned here: https://github.com/gibbok/typescript-book#erased-structural-....
Objects passed to a function that accepts an object type can generally have more properties that needed given Typescript’s structural typing. That is unless you pass a literal directly to the function (I.e. inline the obj variable in the linked example). In which case Typescript will complain about the excess properties. Ergonomic but confusing the first time you come across it.
Those are exact object types which have first class support in flow and no support with exception of literals like you mention in typescript. Flow is ahead in this case.
I believe this implements an exact object type in Typescript:
Credit goes to Manan Tank: https://twitter.com/MananTank_/status/1677610004743086080
No, it doesn't.
It can't be represented in TypeScript type system.
You can't create utility type to have it.
Have a look at Flow docs [0] if you want to learn more.
His utility type works on diff between two provided types, this is something else.
Exact object type is a kind of type which doesn't allow extra, undeclared properties to be used for it to be satisfied.
You can't emulate it if it's not supported by type system.
The whole thing is implemented way better in Flow than TypeScript, ie. spreads map to how runtime treats them, this is also not something TypeScript can represent and Flow supports.
Also opaque types have first class support and many other things.
[0] https://flow.org/en/docs/types/objects/#exact-and-inexact-ob...