Skip to content
This repository was archived by the owner on Dec 21, 2024. It is now read-only.

feat: add support for open interfaces #429

Closed
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
18 changes: 15 additions & 3 deletions packages/backend/toolchain/build/schema/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,17 @@ function generateSerializableTypeSchema(

if (type.isObject()) {
const stringIndexType = type.getStringIndexType();

let indexTypes = [];

if (stringIndexType) {
const indexType = generateSerializableTypeSchema(stringIndexType);
return s.record(indexType);
indexTypes.push(s.record(indexType));
}
const numberIndexType = type.getNumberIndexType();
if (numberIndexType) {
const indexType = generateSerializableTypeSchema(numberIndexType);
return s.record(indexType);
indexTypes.push(s.record(indexType));
}
const properties = type.getProperties();
const propMap = properties.map((prop) => {
Expand All @@ -122,7 +125,14 @@ function generateSerializableTypeSchema(
type ? generateSerializableTypeSchema(type) : s.unknown(),
];
});
return s.object(Object.fromEntries(propMap));

const objectSchema = s.object(Object.fromEntries(propMap));

if (indexTypes[0]) {
return s.intersection(objectSchema, indexTypes[0]);
}

return objectSchema;
}

return s.unknown();
Expand Down Expand Up @@ -260,6 +270,8 @@ interface Array<T> {
[n: number]: T;
}

type PropertyKey = string | number | symbol;

// We don't want to serialize Date from typescript
// so we need to override the Date interface
// to not be serialized, and instead be treated as a Date object
Expand Down
Loading