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

SCCEE for TS type inference issue #278

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/sscce-sequelize-7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
@Attribute(DataTypes.TEXT)
@NotNull
declare name: string;

// Nullable array attribute
@Attribute(DataTypes.ARRAY(DataTypes.STRING))
declare colors?: Array<string>;
}

sequelize.addModels([Foo]);
Expand All @@ -38,6 +42,33 @@
await sequelize.sync({ force: true });
expect(spy).to.have.been.called;

console.log(await Foo.create({ name: 'TS foo' }));
console.log(await Foo.create({ name: 'TS foo', colors: ['red', 'blue'] }));
await Foo.create({ name: 'Colorless Foo' });
expect(await Foo.count()).to.equal(1);

// This works fine and has the expected result, but type-checking fails with

/*
No overload matches this call.
Overload 1 of 2, '(this: ModelStatic<Foo>, options: Omit<FindOptions<InferAttributes<Foo, { omit: never; }>>, "raw"> & { raw: true; }): Promise<...>', gave the following error.
Type '{ colors: null; }' is not assignable to type 'WhereOptions<InferAttributes<Foo, { omit: never; }>>'.
Types of property 'colors' are incompatible.
Type 'null' is not assignable to type 'WhereAttributeHashValue<string[] | undefined>'.
Overload 2 of 2, '(this: ModelStatic<Foo>, options?: FindOptions<InferAttributes<Foo, { omit: never; }>> | undefined): Promise<...>', gave the following error.
Type '{ colors: null; }' is not assignable to type 'WhereOptions<InferAttributes<Foo, { omit: never; }>>'.
Types of property 'colors' are incompatible.
Type 'null' is not assignable to type 'WhereAttributeHashValue<string[] | undefined>'.ts(2769)
model.d.ts(104, 3): The expected type comes from property 'where' which is declared here on type 'Omit<FindOptions<InferAttributes<Foo, { omit: never; }>>, "raw"> & { raw: true; }'
*/

// I _think_ the correct type for where.colors would be WhereAttributeHashValue<string[] | null | undefined> because it is a nullable attribute of the model.
const foosWithoutColors = await Foo.findAll({
where: {

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / mariadb latest (Node 20)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / mysql oldest (Node 20)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / mariadb oldest (Node 20)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / mariadb latest (Node 18)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / mariadb oldest (Node 18)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / mysql oldest (Node 18)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / mssql oldest (Node 20)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / mssql oldest (Node 18)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / mysql latest (Node 18)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / mysql latest (Node 20)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / mssql latest (Node 18)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / mssql latest (Node 20)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / postgres latest (Node 18) (minified aliases)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / postgres latest (Node 18)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / postgres oldest (Node 18)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / postgres latest (Node 20)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / postgres latest (native) (Node 18)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / postgres oldest (native) (Node 18)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / postgres latest (Node 20) (minified aliases)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / postgres oldest (Node 20)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / postgres oldest (native) (Node 18) (minified aliases)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / postgres oldest (Node 18) (minified aliases)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / postgres latest (native) (Node 18) (minified aliases)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / postgres latest (native) (Node 20) (minified aliases)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / postgres latest (native) (Node 20)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / postgres oldest (native) (Node 20)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / sqlite (Node 18)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / sqlite (Node 20)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / postgres oldest (Node 20) (minified aliases)

No overload matches this call.

Check failure on line 66 in src/sscce-sequelize-7.ts

View workflow job for this annotation

GitHub Actions / postgres oldest (native) (Node 20) (minified aliases)

No overload matches this call.
colors: null
}
})

// My type-fu is not good enough to write a unit test asserting that the query type-checks...
// This is expected to pass. The code works fine, it's only a type inference problem.
expect(foosWithoutColors.length === 1)
}
Loading