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

TypeScript does not flag type mismatch in Sequelize findOne method #1734

Open
Co-7 opened this issue Oct 13, 2024 · 1 comment
Open

TypeScript does not flag type mismatch in Sequelize findOne method #1734

Co-7 opened this issue Oct 13, 2024 · 1 comment

Comments

@Co-7
Copy link

Co-7 commented Oct 13, 2024

Hello 👋, first of all, please excuse me if my issue has already been reported, but I don't know what to search for to find it, hence my issue.

I am using the following package versions :

{
  "sequelize": "^6.37.3",
  "sequelize-typescript": "^2.1.6",
  "ts-node": "^10.9.2",
  "typescript": "^5.5.4",
  "typescript-eslint": "^8.0.0",
  "@eslint/js": "^9.8.0",
  "@stylistic/eslint-plugin-ts": "^2.6.1",
  "@types/eslint": "^9.6.0",
  "@types/node": "^22.1.0",
  "@types/validator": "^13.12.0",
  "@typescript-eslint/eslint-plugin": "^8.0.0",
  "@typescript-eslint/parser": "^8.0.0",
  "eslint": "^9.8.0"
}

The problem I'm encountering is that in my findOne method, I set a where clause on the username field and during a refactor, I changed the username to the id but forgot to change the key and only changed the value like this :

const user: User = await User.findOne({
  where: {
    username: claims.id,
  },
});

As you can see in the screenshots, my IDE correctly indicates that username expects a string and claims.id is a number. However, I don't understand why it doesn't flag this as an issue like it usually does, for example, with upsert methods.

  • Type username :

Image

  • Type claims.id :

Image

Once again, I'm not sure if this is the right place to report this, but since it concerns a Sequelize method and TypeScript, I thought you might be able to help. Thank you 🙏

@Co-7
Copy link
Author

Co-7 commented Oct 18, 2024

I detected another similar problem on the findOne where typescript doesn't seem to know that the return of findOne can be null for this code :

const user = await User.findOne({
  where: {
    username: claims.id,
  },
});

Image

Despite the fact that it is well written in the vscode help:

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant