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

Adds DeepReadonly #17

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
41 changes: 41 additions & 0 deletions types/deep-readonly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { DeepReadonly } from "type-zoo";

interface FooType {
bar: string;
baz: string[];
bam: Array<{ bap: string[] }>;
bab: {
bab: {
bab: string;
};
};
}

declare const foo: DeepReadonly<FooType>;

// $ExpectError
foo.bar = "foo";

// $ExpectError
foo.baz = ["hi"];

// $ExpectError
foo.baz.push("baz");

// $ExpectError
foo.bam = [];

// $ExpectError
foo.bam.push({ bap: [] });

// $ExpectError
foo.bab.bab.bab = "hi";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to have some $ExpectTypes as well, to establish the positive properties of this operator.


// $ExpectType DeepReadonlyArray<string>
foo.baz;

// $ExpectType DeepReadonlyObject<{ bab: { bab: string; }; }>
foo.bab;

// $ExpectType DeepReadonlyObject<{ bab: string; }>
foo.bab.bab;
15 changes: 15 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,18 @@ export type Pick4<
};
};
};

/**
* Creates a recursively readonly type
*
* https://github.com/Microsoft/TypeScript/pull/21316
*/
// tslint:disable-next-line: semicolon strict-export-declare-modifiers
export type DeepReadonly<T> = T extends any[] ? DeepReadonlyArray<T[number]> : T extends object ? DeepReadonlyObject<T> : T;

export interface DeepReadonlyArray<T> extends ReadonlyArray<DeepReadonly<T>> {}

export type DeepReadonlyObject<T> = {
// tslint:disable-next-line: semicolon strict-export-declare-modifiers ban-types
readonly [P in { [K in keyof T]: T[K] extends Function ? never : K; }[keyof T]]: DeepReadonly<T[P]>;
};
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ [email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"

"definitelytyped-header-parser@github:Microsoft/definitelytyped-header-parser#production":
definitelytyped-header-parser@Microsoft/definitelytyped-header-parser#production:
version "0.0.0"
resolved "https://codeload.github.com/Microsoft/definitelytyped-header-parser/tar.gz/f074e863231ef0d79a31c0a9edaf1b82c98469ef"
dependencies:
Expand Down Expand Up @@ -309,8 +309,8 @@ typescript@^2.8.1:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.8.1.tgz#6160e4f8f195d5ba81d4876f9c0cc1fbc0820624"

typescript@next:
version "2.8.0-dev.20180127"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.8.0-dev.20180127.tgz#289893b34fc86d6ad572b4cbf01e6df815e22fdf"
version "2.9.0-dev.20180506"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.9.0-dev.20180506.tgz#e20070caa88fca90227cf040459402611a74e13f"

wrappy@1:
version "1.0.2"
Expand Down