Skip to content

Commit

Permalink
Fix FieldTypeFromFieldPath type (#33352)
Browse files Browse the repository at this point in the history
Later versions of TypeScript struggle to realize that `FieldTypeFromFieldPath` should always be a subtype of `Value | undefined`, which was causing type errors around `Expression<FieldTypeFromFieldPath<...>>`

As a quick fix, I wrapped this type in a conditional which more clearly coerces it to be `Value | undefined`.

GitOrigin-RevId: 0c70997fd3c3ceba0fc2ebd7af12352e4eb5942c
  • Loading branch information
sshader authored and Convex, Inc. committed Jan 22, 2025
1 parent 003a55b commit 3f46b0a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/server/data_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,22 @@ type ValueFromUnion<T, Key, Default> = T extends T
export type FieldTypeFromFieldPath<
Document extends GenericDocument,
FieldPath extends string,
> =
FieldTypeFromFieldPathInner<Document, FieldPath> extends Value | undefined
? FieldTypeFromFieldPathInner<Document, FieldPath>
: Value | undefined;

/**
* The inner type of {@link FieldTypeFromFieldPath}.
*
* It's wrapped in a helper to coerce the type to `Value | undefined` since some
* versions of TypeScript fail to infer this type correctly.
*
* @public
*/
export type FieldTypeFromFieldPathInner<
Document extends GenericDocument,
FieldPath extends string,
> = FieldPath extends `${infer First}.${infer Second}`
? ValueFromUnion<
Document,
Expand Down
1 change: 1 addition & 0 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export type {
GenericVectorIndexConfig,
GenericTableVectorIndexes,
FieldTypeFromFieldPath,
FieldTypeFromFieldPathInner,
GenericTableInfo,
DocumentByInfo,
FieldPaths,
Expand Down

0 comments on commit 3f46b0a

Please sign in to comment.