Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update createDefaultCodec to handle Unknown types correctly #35

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -117,7 +117,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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be UnknownRecord, since UnknownType is for unknown

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, if I do codec instanceof t.RecordType I get error Right-hand side of 'instanceof' is not callable

return {} as t.TypeOf<C>;
}

Expand Down
6 changes: 6 additions & 0 deletions src/tests/createDefaultCodec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ describe('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
Loading