Meteor-Community-Packages / Meteor-Community-Packages/meteor-simple-schema
Make SimpleSchema class Generic for typing infering
- Dominant language
- JavaScript
- Stars
- 914
- Forks
- 162
- PR merge metrics
- No merged PRs in 30d
Description
**Is your feature request related to a problem? Please describe.**
We work on a typescript project and I often find myself needing to declare schema with `new SimpleSchema(objectSchema)` and also declaring the type in typescript
Example:
```ts
// A silly example
const chairSchema = new SimpleSchema({
constructionDate: {
type: Date,
autoValue() {
return new Date();
},
},
legsCount: Number,
});
type ChairToInsert = {
/**
* Optional since there is an autovalue
*/
constructionDate?: Date;
legsCount: number;
};
type ChairInDatabase = {
_id: string;
/**
* When we fetch for the document in database, the autovalue is set so we get the value
*/
constructionDate: Date;
legsCount: number;
};
```
I found that it's quite redundant since all informations are contained in the schema and it can be painful to maintain in a large scale project.
**Describe the solution you'd like**
I think it would be great to have a generic schema type
```ts
class SimpleSchema {
constructor(schema: TSchema, options?: SimpleSchemaOptions){
// Work here
}
}
```
And also some type helpers that parses the `TSchema` type to provide some useful types such as the type inserted and the type received
(not completed) Example:
```ts
type OutTypeFromObj = {
[k in keyof T]: OutTypeFromAny;
};
/** TODO */
type OutTypeFromAny = any;
/**
* Provides the output type from the database
*/
type OutTypeFromSchema = T extends SimpleSchema ? OutTypeFromObj : never;
```
It would be a nice to have:
- Input type from schema
- Output type
That takes into account the `defaultValue`, `autoValue`, and 'optional' fields in the schema
**Describe alternatives you've considered**
Some packages provides such flexibility such as `zod`, that provides some typing helpers `type MyObject = z.infer`
Contributor guide
Research direction
Start by locating the existing SimpleSchema, SimpleSchemaDefinition, and SimpleSchemaOptions declarations and reviewing how the JavaScript package exposes types. The work is complete when a generic schema can provide input and output helper types that account for defaultValue, autoValue, and optional fields, with coverage for the supported schema forms.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- developer-experience
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100