From 3f46b0a4b174076b6baf01a25df5994f22e97727 Mon Sep 17 00:00:00 2001 From: Sarah Shader Date: Tue, 21 Jan 2025 19:08:30 -0500 Subject: [PATCH] Fix FieldTypeFromFieldPath type (#33352) Later versions of TypeScript struggle to realize that `FieldTypeFromFieldPath` should always be a subtype of `Value | undefined`, which was causing type errors around `Expression>` As a quick fix, I wrapped this type in a conditional which more clearly coerces it to be `Value | undefined`. GitOrigin-RevId: 0c70997fd3c3ceba0fc2ebd7af12352e4eb5942c --- src/server/data_model.ts | 16 ++++++++++++++++ src/server/index.ts | 1 + 2 files changed, 17 insertions(+) diff --git a/src/server/data_model.ts b/src/server/data_model.ts index 0552a0a..27e6ba7 100644 --- a/src/server/data_model.ts +++ b/src/server/data_model.ts @@ -104,6 +104,22 @@ type ValueFromUnion = T extends T export type FieldTypeFromFieldPath< Document extends GenericDocument, FieldPath extends string, +> = + FieldTypeFromFieldPathInner extends Value | undefined + ? FieldTypeFromFieldPathInner + : 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, diff --git a/src/server/index.ts b/src/server/index.ts index b84a66f..7a28b06 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -63,6 +63,7 @@ export type { GenericVectorIndexConfig, GenericTableVectorIndexes, FieldTypeFromFieldPath, + FieldTypeFromFieldPathInner, GenericTableInfo, DocumentByInfo, FieldPaths,