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 1 commit
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
32 changes: 32 additions & 0 deletions types/deep-readonly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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.

37 changes: 37 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,40 @@ export type Pick4<
};
};
};

// https://github.com/Microsoft/TypeScript/pull/21316

/**
* Extracts union strings of all property names in T whose value is a function.
*/
// tslint:disable-next-line: semicolon strict-export-declare-modifiers ban-types
export type FunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Function ? K : never; }[keyof T];

/**
* Creates a type that only includes the properties which are functions in T.
*/
export type FunctionProperties<T> = Pick<T, FunctionPropertyNames<T>>;

/**
* Extracts union strings of all property names in T whose value is not a function.
*/
// tslint:disable-next-line: semicolon strict-export-declare-modifiers ban-types
export type NonFunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Function ? never : K; }[keyof T];

/**
* Creates a type that only includes the properties which are functions in T.
*/
export type NonFunctionProperties<T> = Pick<T, NonFunctionPropertyNames<T>>;
Copy link
Owner

@pelotom pelotom May 8, 2018

Choose a reason for hiding this comment

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

These don't seem to me to be of general enough utility to define them as top-level exports. I would get rid of FunctionPropertyNames/FunctionProperties since they aren't needed in the definition of DeepReadonly, and inline NonFunctionPropertyNames/NonFunctionProperties.


/**
* Creates a recursively readonly type
*/
// 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>> {}

// tslint:disable-next-line: semicolon strict-export-declare-modifiers
export type DeepReadonlyObject<T> = {
readonly [P in NonFunctionPropertyNames<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 @@ concat-map@0.0.1:
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