-
Notifications
You must be signed in to change notification settings - Fork 11
Open
Description
I have this basic getKeys
function:
/** Strongly typed version of `Object.keys(object)` */
export function getKeys<ObjectType extends Record<string, any>>(object: ObjectType | undefined | null) {
return Object.keys(object || {}) as Array<keyof ObjectType>;
}
And I have written these tests:
describe("getKeys", () => {
it("should return an array of correctly typed keys", () => {
const keysFromGetKeys = getKeys({ a: 1, b: "2" });
const keysFromObjectKeys = Object.keys({ a: 1, b: "2" });
expect(keysFromGetKeys).toEqual(["a", "b"]);
expect(keysFromObjectKeys).toEqual(["a", "b"]);
// These should cause the test to fail
expectTypeOf(keysFromGetKeys).toEqualTypeOf<Array<string>>();
expectTypeOf(keysFromObjectKeys).toEqualTypeOf<Array<"a" | "b">>();
// these should pass
expectTypeOf(keysFromGetKeys).toEqualTypeOf<Array<"a" | "b">>();
expectTypeOf(keysFromObjectKeys).toEqualTypeOf<Array<string>>();
});
});
However the vitest tests are still passing
I did add the --typecheck
parameter to the npm "test": "vitest --typecheck"
script.
I am using vitest for testing. Vitest is using expect-type for type testing.
I have installed the latest version of expect-type to ensure it isn't an issue of vitest using an outdated version.
I'm not sure if this is a bug in Vitest or a bug in expect-type.
Bug created against Vitest here:
vitest-dev/vitest#7691
Metadata
Metadata
Assignees
Labels
No labels