Fix TypeScript error in useQueryBuilder test#31
Conversation
Add type assertion for serialized filter result to fix TS18046 error where 'serialized' was of type 'unknown'. Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
Define SerializedFilter interface for better type safety when testing the serialize() method. Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR addresses a TypeScript compile error in the useQueryBuilder hook test by introducing a local type for the mocked serialize() output and casting the unknown return value to access logic.
Changes:
- Add a
SerializedFilterinterface in the test file to describe the mocked serialized shape. - Update the serialization assertion to use a type assertion when accessing
logic.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const serialized = result.current.serialize(); | ||
| expect(serialized).toBeDefined(); | ||
| expect(serialized.logic).toBe("AND"); | ||
| expect((serialized as SerializedFilter).logic).toBe("AND"); |
There was a problem hiding this comment.
expect(serialized).toBeDefined() doesn’t exclude null (null is “defined”), so if serialize() ever returns null this test will throw when accessing .logic rather than failing with a clear assertion. Prefer asserting non-null (e.g., not.toBeNull() / not.toBeNull() + type guard) or use an objectContaining({ logic: "AND" }) matcher to avoid unsafe property access on unknown.
| /* ---- Types ---- */ | ||
| interface SerializedFilter { | ||
| logic: string; | ||
| filters: Array<{ | ||
| field: string; | ||
| operator: string; | ||
| value: string; | ||
| }>; | ||
| } |
There was a problem hiding this comment.
The new SerializedFilter interface suggests the real ObjectQL serialization format is an object with logic/filters, but the production serializeFilterTree() returns an array-based AST (unknown | null). To avoid confusion, consider renaming this to something test/mock-specific (e.g., MockSerializedFilter) or switching the expectation to structural matching without introducing a misleading type name.
CI failing on
tsc --noEmitdue to accessing property onunknowntype.serializeFilterTree()returnsunknown | nullper ObjectQL spec (supports multiple array shapes). Test mock returns structured object but TypeScript doesn't know this.Changes
SerializedFilterinterface matching mock return type(serialized as SerializedFilter).logicOriginal prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.