papr icon indicating copy to clipboard operation
papr copied to clipboard

More type-safe `projection` type

Open ejmartin504 opened this issue 2 years ago • 1 comments

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)

ejmartin504 avatar Dec 15 '23 19:12 ejmartin504

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.

joshuat avatar Jan 19 '24 02:01 joshuat