Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assigning proberty to variable of type UpdateQuery returns error read-only #10786

Closed
GamelSadek opened this issue Sep 26, 2021 · 2 comments
Closed
Labels
typescript Types or Types-test related issue / Pull Request

Comments

@GamelSadek
Copy link

Do you want to request a feature or report a bug?
bug
What is the current behavior?
declaring a variable with type UpdateQuery if you assign a value to this variable it returns the error "Cannot assign to ' ' because it is a read-only property"
If the current behavior is a bug, please provide the steps to reproduce.

import mongoose, { Schema, model,  UpdateQuery } from 'mongoose';
async function run() {
    mongoose.connect('mongodb://localhost:27017/test');
    interface User {
        phone?: string; 
        name?: string
    }
    const UserModel = model<User>('User', new Schema<User>({
        phone: String,
        name: String
    }));
    const updateQuery : UpdateQuery<User> = {name :'John'};
    if (1==1) {
        updateQuery.phone = 'XXXX'
    }
    const user = await UserModel.updateOne({},updateQuery)
}
{
  "compilerOptions": {
    "target": "ES6",                      
    "module": "commonjs",                     
    "strict": true,                          
    "esModuleInterop": true,                 
    "forceConsistentCasingInFileNames": true 
  }
}

What is the expected behavior?
it should not return an error

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js: 14.17.4 , Mongoose: 6.0.7 , MongoDB: 4.4.3

@AbdelrahmanHafez AbdelrahmanHafez added the typescript Types or Types-test related issue / Pull Request label Sep 27, 2021
@vkarpov15 vkarpov15 added this to the 6.0.11 milestone Sep 27, 2021
@jdegger
Copy link

jdegger commented May 26, 2023

@vkarpov15 This bug still exists for when your query is using a $set:

const alarmUpdate: UpdateQuery<Alarm> = {
  $set: {
    severity: alarm.severity,
  },
};

if(true){ 

  alarmUpdate.$set.severity = dto.severity;
  
}

Gives readonly error.

@vkarpov15 vkarpov15 reopened this May 26, 2023
@vkarpov15 vkarpov15 modified the milestones: 6.0.11, 7.2.3 May 26, 2023
@vkarpov15
Copy link
Collaborator

@jdegger please open a new issue and follow the issue template, you're missing a lot of key information, including mongoose version, typescript version, typescript config, etc.

Below script compiles without error on Mongoose 7.2.2, TypeScript 4.9.4.

import { UpdateQuery } from 'mongoose';

interface Alarm {
  severity: string;
}

const alarmUpdate: UpdateQuery<Alarm> = {
  $set: {
    severity: 'foo',
  },
};

if(true){ 

  alarmUpdate.$set!.severity = 'bar';
  
}

@vkarpov15 vkarpov15 removed this from the 7.2.3 milestone Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

4 participants