Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing UpdateData<T> to expand support for types with index signatures. #7318

Merged
merged 14 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion common/api-review/firestore-lite.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function addDoc<T>(reference: CollectionReference<T>, data: WithFieldValu

// @public
export type AddPrefixToKeys<Prefix extends string, T extends Record<string, unknown>> = {
[K in keyof T & string as `${Prefix}.${K}`]+?: T[K];
[K in keyof T & string as `${Prefix}.${K}`]+?: string extends K ? any : T[K];
};

// @public
Expand Down
2 changes: 1 addition & 1 deletion common/api-review/firestore.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function addDoc<T>(reference: CollectionReference<T>, data: WithFieldValu

// @public
export type AddPrefixToKeys<Prefix extends string, T extends Record<string, unknown>> = {
[K in keyof T & string as `${Prefix}.${K}`]+?: T[K];
[K in keyof T & string as `${Prefix}.${K}`]+?: string extends K ? any : T[K];
};

// @public
Expand Down
12 changes: 11 additions & 1 deletion packages/firestore/src/lite-api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,17 @@ export type AddPrefixToKeys<
T extends Record<string, unknown>
> =
// Remap K => Prefix.K. See https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#key-remapping-via-as
{ [K in keyof T & string as `${Prefix}.${K}`]+?: T[K] };

// `string extends K : ...` is used to detect index signatures
// like `{[key: string]: bool}`. We map these properties to type `any`
// because a field path like `foo.[string]` will match `foo.bar` or a
// sub-path `foo.bar.baz`. Because it matches a sub-path, we have to
// make this type `any` to allow for any types of the sub-path property.
// This is a significant downside to using index signatures in types for `T`
// for `UpdateData<T>`.

// eslint-disable-next-line @typescript-eslint/no-explicit-any
{ [K in keyof T & string as `${Prefix}.${K}`]+?: string extends K ? any : T[K] };

/**
* Given a union type `U = T1 | T2 | ...`, returns an intersected type
Expand Down
Loading