plexinc / papr

MongoDB TypeScript-aware Models
https://plexinc.github.io/papr/
MIT License
445 stars 23 forks source link

More type-safe `projection` type #683

Open ejmartin504 opened 9 months ago

ejmartin504 commented 9 months ago

Consider the following schema:

const mySchema = schema(
  {
    foo: Types.string(),
    bar: Types.string()
});

const MyModel = papr.model('mymodels', mySchema);

Now consider some code that queries this model:

const t = MyModel.find(
  { foo: 'baz' },
  {
    projection: {
      foo: 1,
      unknown: 1
    }
  }
);

Currently this projection type will not produce a TS error even though it is projecting properties that do not exist on the schema. This can be problematic in applications, especially because it will not flag misspelled properties (an issue we ran into recently)

joshuat commented 8 months ago

I bet this is being caused by the combination of using a Partial on the projection type definition and having the projection type defined in the generic so we can use it in the return value.