Typescript CreationAttributes are always being marked as optional with strictNullChecks disabled

Open
#72 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
typescript
Domain
api

Research direction

No repository files or tests are identified. Start by reproducing the supplied Sequelize model example with TypeScript 4.5.4 and strictNullChecks disabled, then trace the creation-attribute typings involved in Model creation. Done means omitting required name or height produces a TypeScript error while the optional id remains allowed.

Written by the indexing model from the issue text.

Description

Issue Creation Checklist

Bug Description

Typescript CreationAttributes are being marked as optional regardless of actually marking them as so. When creating a model typescript does not complain about some required parameters that are missing because of this.

Example
import { DataTypes, Model, Optional, Sequelize } from "sequelize"

interface Attributes {
    id: string
    name: string
    height: number
}

type CreationAttributes = Optional<Attributes, "id">

export default class Person extends Model<Attributes, CreationAttributes> implements Attributes {
    public id!: string
    public name!: string
    public height!: number

    public static initialize(sequelize: Sequelize) {
        this.init({
            id: {
                type: DataTypes.UUID,
                defaultValue: DataTypes.UUIDV4,
                field: "ID",
                primaryKey: true,
                allowNull: false,
            },
            name: {
                type: DataTypes.STRING,
                field: "Name",
                allowNull: false,
            },
            height: {
                type: DataTypes.NUMBER,
                field: "Height",
                allowNull: false,
            }
        }, {
            sequelize: sequelize,
            tableName: "Person"
        })
    }
}
What do you expect to happen?

It should throw a typescript error if Person.name and Person.height are not provided when creating a Person.

What is actually happening?

Typescript thinks name is optional even though it's being marked as required.
Screen Shot 2022-04-06 at 3 09 48 PM

Typescript does not complain when required fields are omitted.
Screen Shot 2022-04-06 at 3 10 09 PM

Environment
  • Sequelize version: sequelize@6.18.0
  • Node.js version: v14.19.1
  • ITypeScript version: typescript@4.5.4

Would you be willing to resolve this issue by submitting a Pull Request?

  • No, I don't have the time, although I believe I could do it if I had the time...
Dominant language
HTML
Stars
30
Forks
170
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

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.

More from sequelize/website

All issues in sequelize/website

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.