Type-safety is broken on create/update/remove and more
See original GitHub issueVersions
-
System: Dockerized nodejs
-
NodeJS: 16.15.1 (tried even 18)
-
Typescript: 4.7.3
-
Typegoose(NPM): 9.9.0 (tried also to downgrade)
-
mongoose: 6.3.5 (tried also to downgrade)
was using transpileOnly with ts-node-dev, right now with nodemon -r
What is the Problem?
I’ve been using typegoose since a while and until a few week ago, everything had always worked fine. Right now, in every my project using typegoose, it looks like type-safety is broken. To be specific, the types are correctly inferred from the model (and so the results from find for ex.), but the type-safety of update/create/delete methods are literally broken. What i mean, is that i am able to create or update an empty document even if the model is correctly defined. This was not happening before, as i was receiving errors immediately if i were performing a query with the wrong types and/or were creating a document with non-valid keys.
The issue occurs even during build time, where despite having some wrong queries the build pass with no issues.
Code Example
Model declaration
import { modelOptions, prop, Severity, pre, DocumentType, getModelForClass, Ref } from "@typegoose/typegoose";
import { User } from "model/user";
@pre<SamlAuthRequests>("save", async function () {
return;
})
@modelOptions({
schemaOptions: {
timestamps: true,
collection: 'saml.auth.requests'
},
options: {
allowMixed: Severity.ALLOW
}
})
export class SamlAuthRequests {
@prop({ required: true })
authCode!: string
@prop({ required: false, default: true })
userAllowed?: boolean
@prop({ required: false })
errorMessage?: string
@prop({ ref: () => User, required: false })
user_id?: Ref<User>
}
const SamlAuthRequestsModel = getModelForClass(SamlAuthRequests)
export default SamlAuthRequestsModel
Model usage
await SamlAuthRequestsModel.updateOne({}, { lorem: 'ipsum' })
await SamlAuthRequestsModel.create({ lorem: 'ipsum' })
await SamlAuthRequestsModel.create()
await SamlAuthRequestsModel.create({})
// All of this works, despite it shouldn't.
// When i started using typegoose, i received errors by doing something like this.
// If i perform these calls the documents are inserted in the collection succesfully
Thanks in advance for your time, hope there is a solution to this because it’s not possible to write correct code without this type-safety check.
Issue Analytics
- State:
- Created a year ago
- Comments:8 (3 by maintainers)
this is not a issue with typegoose from what i can tell, but with the types mongoose now provides.
currently mongoose allows any (even non-existent) keys in the
create
/new
without error and only works as a suggestion, the only workaround currently is to do aas Class
(if it wants function, they will have to be removed)to which versions were you trying to downgrade to?
Closing Issue because it is marked as stale