Skip to content

Commit

Permalink
update createDefaultCodec to handle UnknownType
Browse files Browse the repository at this point in the history
  • Loading branch information
abrantesarthur committed Jan 19, 2025
1 parent cd336a0 commit 4c0bb04
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Transcend Inc.",
"name": "@transcend-io/type-utils",
"description": "Small package containing useful typescript utilities.",
"version": "1.8.3",
"version": "1.8.4",
"homepage": "https://github.com/transcend-io/type-utils",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/codecTools/createDefaultCodec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const createDefaultCodec = <C extends t.Mixed>(
}

// The default of an object type is an empty object
if (codec instanceof t.ObjectType) {
if (codec instanceof t.ObjectType || codec instanceof t.UnknownType) {
return {} as t.TypeOf<C>;
}

Expand Down
8 changes: 7 additions & 1 deletion src/tests/createDefaultCodec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createDefaultCodec } from '../codecTools';

chai.use(deepEqualInAnyOrder);

describe.only('buildDefaultCodec', () => {
describe('buildDefaultCodec', () => {
it('should correctly build a default codec for null', () => {
const result = createDefaultCodec(t.null);
expect(result).to.equal(null);
Expand Down Expand Up @@ -44,6 +44,12 @@ describe.only('buildDefaultCodec', () => {
expect(defaultCodec).to.deep.equal({});
});

it('should correctly build a default codec for an UnknownType', () => {
const codec = t.unknown;
const defaultCodec = createDefaultCodec(codec);
expect(defaultCodec).to.deep.equal({});
});

it('should correctly build a default codec for a union with null', () => {
const result = createDefaultCodec(t.union([t.string, t.null]));
// should default to null if the union contains null
Expand Down

0 comments on commit 4c0bb04

Please sign in to comment.