In the TS-SDK we duplicate the type and guard definition for each type as the
inferred type from io-ts does not produce a good type export when used with
typedoc. Sadly this means we have to maintain two definitions for each type which
can lead to errors, so we need a mechanism to assert that the type and guard are
equal.
Normally we could simply do:
And the explicit type assertion in the FooGuard ensure us that the type are the same.
But when we include Branded or Newtype types in the definition, the full type is not the same
interfaceFoo { prop: ContractId; } // This throws a type error constFooGuard: t.Type<Foo> = t.type({ prop:ContractIdGuard })
To understand the error we need to know that t.Type<A> = t.Type<A, O = A, I = unknown>
where A is the underlying type, O is the Output type that we get when
we call FooGuard.encode and I is the Input type we expect when we call FooGuard.decode.
When we use a Branded or Newtype guard, the Output type is different from the underlying type,
so we get an error.
The assertGuardEqual function can be used to only assert on the representation type A and leave
the Input and Output type unchanged.
In the TS-SDK we duplicate the type and guard definition for each type as the inferred type from io-ts does not produce a good type export when used with typedoc. Sadly this means we have to maintain two definitions for each type which can lead to errors, so we need a mechanism to assert that the type and guard are equal. Normally we could simply do:
And the explicit type assertion in the FooGuard ensure us that the type are the same. But when we include Branded or Newtype types in the definition, the full type is not the same
To understand the error we need to know that
t.Type<A> = t.Type<A, O = A, I = unknown>
whereA
is the underlying type,O
is the Output type that we get when we callFooGuard.encode
andI
is the Input type we expect when we callFooGuard.decode
. When we use a Branded or Newtype guard, the Output type is different from the underlying type, so we get an error.The
assertGuardEqual
function can be used to only assert on the representation type A and leave the Input and Output type unchanged.