Skip to content

Commit 8bdcc18

Browse files
committed
Address review: explain why CborType | ReadonlyCborType union is needed
bartlomieju noted on #7148 that the union looks redundant at first glance because mutable Map/array/index-signature are all assignable to their readonly counterparts. The reason it is needed is that CborTag<T> has a writable tagContent field and is therefore invariant in T, so CborTag<CborType> is not assignable to CborTag<ReadonlyCborType> and the union preserves existing CborTag callers. Document that on ReadonlyCborType so the union is not 'simplified' away later.
1 parent 8df2d62 commit 8bdcc18

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

cbor/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ export type CborType =
5959
* encodeCbor(data);
6060
* ```
6161
*/
62+
// Note: encoder signatures take `CborType | ReadonlyCborType` rather than
63+
// just `ReadonlyCborType`. Mutable `Map`/array/index-signature are all
64+
// assignable to their readonly counterparts, so the union looks redundant,
65+
// but `CborTag<T>` has a writable `tagContent: T` field, which makes it
66+
// invariant in `T`. That means `CborTag<CborType>` is NOT assignable to
67+
// `CborTag<ReadonlyCborType>`, and existing callers passing
68+
// `CborTag<CborType>` instances would break if the union were collapsed.
6269
export type ReadonlyCborType =
6370
| CborPrimitiveType
6471
| CborTag<ReadonlyCborType>

0 commit comments

Comments
 (0)