Skip to content

Commit

Permalink
SCCEE for TS type inference issue
Browse files Browse the repository at this point in the history
This SCCEE is expected to produce an incorrect type error when querying with `null` in the `where` clause for a nullable field. The code itself works fine, the query returns the expected results.
  • Loading branch information
RJFelix authored Jun 22, 2024
1 parent c8b54c3 commit b8a036d
Showing 1 changed file with 32 additions and 1 deletion.
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 @@ export async function run() {
@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 @@ export async function run() {
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)
}

0 comments on commit b8a036d

Please sign in to comment.