Add support for `t.intersection` and `t.keyof` Types
Closed this issue · 1 comments
Describe the bug
When using t.intersection
and t.keyof
from the io-ts library (version 2.2.20) with the polyfact SDK (version 0.1.7) in a Node.js (v19.4.0) environment, an error message saying "Unsupported type '[object Object]'." is thrown.
To Reproduce
The error can be reproduced by using the following code:
const TReference = t.type({
name: t.string,
category: t.keyof({
function: null,
method: null,
class: null,
type: null,
structure: null,
enum: null,
}),
});
const TReferenceWithSubreferences = t.intersection([
TReference,
t.type({
subreferences: t.array(TReference),
}),
]);
Expected behavior
The expected behavior is that the types t.intersection
and t.keyof
should be handled without any error.
Actual behavior
The actual behavior is that an error is thrown:
Error: Unsupported type "[object Object]".
Please use one of:
- InterfaceType (t.type)
- ArrayType (t.array)
- NumberType (t.number)
- StringType (t.string)
- BooleanType (t.boolean)
Proposed Fix
Based on the error message and provided code, it seems the internalTsio2JSON
function currently doesn't support the IntersectionType
and KeyofType
from io-ts. A potential fix could involve adding additional checks to handle these types within the function.
Here's a preliminary suggestion on how this could be approached:
function internalTsio2JSON(type: any): any {
// ... Existing conditions ...
// Additional handlers for IntersectionType and KeyofType
if (type._tag === "IntersectionType") {
return type.types.map(internalTsio2JSON);
}
if (type._tag === "KeyofType") {
return Object.keys(type.keys);
}
throw new Error(
`Unsupported type "${type}".\nPlease use one of:\n\t- InterfaceType (t.type)\n\t- ArrayType (t.array)\n\t- NumberType (t.number)\n\t- StringType (t.string)\n\t- BooleanType (t.boolean)\n\t- IntersectionType (t.intersection)\n\t- KeyofType (t.keyof)\n`,
);
}
Additional context
My project is written in TypeScript and uses io-ts for static type checking at runtime.
Thank you for looking into this issue.