Skip to content
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
45 changes: 36 additions & 9 deletions packages/effect-firebase/src/lib/firestore/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,50 @@ import {
// ============================================================================

/**
* Extract field keys from a Schema struct (Model).
* Extract field keys from a Schema struct (Model), including nested fields as dot notation strings.
*
* For example, given a schema:
* ```ts
* const PostSchema = Schema.Struct({
* title: Schema.String,
* author: Schema.Struct({
* name: Schema.String,
* age: Schema.Number
* })
* });
*
* type PostSchema = Schema.Schema.Type<typeof PostSchema>
* type Keys = FieldKeys<PostSchema> // "title" | "author.name" | "author.age"
```
*/
export type FieldKeys<S> = S extends { readonly fields: infer F }
? keyof F & string
: never;
type DotPrefix<T extends string> = T extends '' ? '' : `.${T}`
export type FieldKeys<S> = (
S extends object ?
{ [K in Exclude<keyof S, symbol>]: `${K}${DotPrefix<FieldKeys<S[K]>>}` }[Exclude<keyof S, symbol>]
: '') extends infer D ? Extract<D, string> : never

/**
* Extract the type of a specific field from a Schema.
* Extract the type of a specific field from a Schema, including nested fields.
*
* For example,
* `FieldType<typeof PostSchema, "title">` would be `string`.
* `FieldType<typeof PostSchema, "title2">` TypeError.
* `FieldType<typeof PostSchema, "author.name">` would be `string`.
* `FieldType<typeof PostSchema, "author.age">` would be `number`.
*/
export type FieldType<S, K extends string> = S extends {
export type FieldType<S, K extends FieldKeys<S>> = S extends {
readonly Type: infer T;
}
? K extends keyof T
? T[K]
: never
? FieldType<T, K>
: K extends `${infer Head}.${infer Tail}`
? Head extends keyof S
? FieldType<S[Head], FieldKeys<S[Head]>>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Nested path tail is discarded

When querying a nested field whose siblings have different types, this branch passes every key below Head instead of the inferred Tail, so FieldType<S, "author.name"> becomes a sibling-type union and accepts invalid query values such as a number for a string field.

Suggested change
? FieldType<S[Head], FieldKeys<S[Head]>>
? FieldType<S[Head], Extract<Tail, FieldKeys<S[Head]>>>

Fix in Claude Code

: never
: K extends keyof S
? S[K]
: never;


// ============================================================================
// Query Builder Type
// ============================================================================
Expand Down
Loading