Skip to content

Commit c89a042

Browse files
hanslmgechev
authored andcommitted
feat(@angular-devkit/core): add DeepReadonly<> and Readwrite<> types
Readwrite removes readonly, while DeepReadonly recursively sets it. A ReadonlyArray should be used if the type is an array, but that is too much of a change to our API (JsonValue includes JsonArray but then we would need a ReadonlyJsonValue, and same for object, which has deep roots in our API). We should fix this in a further refactor at some point.
1 parent 64781f6 commit c89a042

File tree

1 file changed

+17
-0
lines changed
  • packages/angular_devkit/core/src/utils

1 file changed

+17
-0
lines changed

packages/angular_devkit/core/src/utils/index.ts

+17
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,20 @@ export * from './priority-queue';
1616
export * from './lang';
1717

1818
export { tags, strings };
19+
20+
export type DeepReadonly<T> =
21+
T extends (infer R)[] ? DeepReadonlyArray<R> :
22+
T extends Function ? T :
23+
T extends object ? DeepReadonlyObject<T> :
24+
T;
25+
26+
// This should be ReadonlyArray but it has implications.
27+
export interface DeepReadonlyArray<T> extends Array<DeepReadonly<T>> {}
28+
29+
export type DeepReadonlyObject<T> = {
30+
readonly [P in keyof T]: DeepReadonly<T[P]>;
31+
};
32+
33+
export type Readwrite<T> = {
34+
-readonly [P in keyof T]: T[P];
35+
};

0 commit comments

Comments
 (0)