question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type-safety is broken on create/update/remove and more

See original GitHub issue

Versions

  • 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:closed
  • Created a year ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
hasezoeycommented, Jun 15, 2022

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.

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 a as Class (if it wants function, they will have to be removed)

(tried also to downgrade)

to which versions were you trying to downgrade to?

0reactions
github-actions[bot]commented, Aug 1, 2022

Closing Issue because it is marked as stale

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fixing common type problems - Dart
Fixing common type problems · Undefined member · Invalid method override · Missing type arguments · Unexpected collection element type · Constructor initialization ......
Read more >
Type safety: Unchecked cast - java - Stack Overflow
The problem is that a cast is a runtime check - but due to type erasure, at runtime there's actually no difference between...
Read more >
PHP or Type Safety: Pick any two - Psalm
In some languages (like Haskell and StandardML), type safety is pretty much guaranteed — the program won't compile unless types are used ...
Read more >
Documentation - Narrowing - TypeScript
The idea is that TypeScript's type system aims to make it as easy as possible to write typical JavaScript code without bending over...
Read more >
Troubleshoot problems updating Windows - Microsoft Support
Before you begin, make sure your device is plugged into a power source and connected to the internet. Then, try the steps listed...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found