Skip to content

feat(client-core-interfaces): DeepReadonly and ShallowReadonly utility types #24265

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

Merged
merged 4 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
68 changes: 68 additions & 0 deletions packages/common/core-interfaces/src/deepReadonly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/

import type {
DeepReadonlyRecursionLimit,
InternalUtilityTypes,
ReadonlySupportedGenerics,
} from "./exposedInternalUtilityTypes.js";

/**
* Default set of generic that {@link DeepReadonly} will apply deep immutability
* to generic types.
*
* @privateRemarks
* WeakRef should be added when lib is updated to ES2021 or later.
*
* @system
*/
export type DeepReadonlySupportedGenericsDefault =
| Map<unknown, unknown>
| Promise<unknown>
| Set<unknown>
| WeakMap<object, unknown>
| WeakSet<object>;

/**
* Options for {@link DeepReadonly}.
*
* @beta
*/
export interface DeepReadonlyOptions {
/**
* Union of Built-in and IFluidHandle whose generics will also be made deeply immutable.
*
* The default value is `Map` | `Promise` | `Set` | `WeakMap` | `WeakSet`.
*/
DeepenedGenerics?: ReadonlySupportedGenerics;

/**
* Limit on processing recursive types.
*
* The default value is `"NoLimit"`.
*/
RecurseLimit?: DeepReadonlyRecursionLimit;
}

/**
* Transforms type to a fully and deeply immutable type, with limitations.
*
* @beta
*/
export type DeepReadonly<
T,
Options extends DeepReadonlyOptions = {
DeepenedGenerics: DeepReadonlySupportedGenericsDefault;
RecurseLimit: "NoLimit";
},
> = InternalUtilityTypes.DeepReadonlyImpl<
T,
Options extends { DeepenedGenerics: unknown }
? Options["DeepenedGenerics"]
: DeepReadonlySupportedGenericsDefault,
Options extends { RecurseLimit: DeepReadonlyRecursionLimit }
? Options["RecurseLimit"]
: "NoLimit"
>;
Loading
Loading