sequelize / sequelize/sequelize

TypeScript - Cannot access static members of model with .schema() call

Open
#12,322 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

typescript
Dominant language
TypeScript
Stars
30.4k
Forks
4.3k
Avg merge
1d 6h
Merged PRs (30d)
68

Description

Issue Description

When you call .schema() method of model then call static member typescript says that member doesn't exist.

What are you doing?
class TestModelWithStaticMethod extends Model {
  static someStaticFn() {
    return 'hello!';
  }
};
TestModelWithStaticMethod.init({ hello: { type: DataTypes.BOOLEAN, allowNull: false } }, { sequelize });
const testModelWithStaticMethod = TestModelWithStaticMethod.schema('some_schema');
testModelWithStaticMethod.someStaticFn();
What do you expect to happen?

Ability to use static members of model without warnings.

What is actually happening?
> tsc -b types/tsconfig.json && tsc -b types/test/tsconfig.json

types/test/model.ts:86:27 - error TS2339: Property 'someStaticFn' does not exist on type '(new () => TestModelWithStaticMethod) & typeof Model'.

86 testModelWithStaticMethod.someStaticFn();
Environment
  • Sequelize version: 5.x and 6.x
  • Node.js version: v13.2.0
  • Operating System: macOS 10.15.3
  • If TypeScript related: TypeScript version: 3.7.2

Issue Template Checklist

How does this problem relate to dialects?
  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s):
  • I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX
Would you be willing to resolve this issue by submitting a Pull Request?
  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the failing example in types/test/model.ts and the TypeScript build commands shown in the error. Trace the type returned by Model.schema(), then update the typing so subclass static members remain available and confirm both listed TypeScript builds pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
nodejs, typescript
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.