sequelize/sequelize-typescript

TypeScript does not flag type mismatch in Sequelize findOne method

Co-7 opened this issue ยท 1 comments

Co-7 commented

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 commented

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