Skip to content

Commit

Permalink
Fix implementation of .readonly()
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaemami59 committed Oct 9, 2024
1 parent b696639 commit 5a8b2dc
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 50 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,6 @@ type Post = {title: string; content: string}
expectTypeOf<Post>().readonly().toEqualTypeOf<Readonly<Post>>()
```

`.readonly` can make specific properties `readonly`:

```typescript
type Post = {title: string; content: string}

expectTypeOf<Post>().readonly('title').toEqualTypeOf<{readonly title: string; content: string}>()
```

Make assertions about object properties:

```typescript
Expand Down
38 changes: 6 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,13 +676,11 @@ export interface BaseExpectTypeOf<Actual, Options extends {positive: boolean}> {
) => ExpectTypeOf<Omit<Actual, KeyToOmit>, Options>

/**
* Converts specified properties of an object to `readonly`.
* If no properties are specified, it defaults
* to making all properties `readonly`, similar to the native
* TypeScript {@linkcode Readonly} utility type.
* Converts all properties of a type to **`readonly`**,
* behaving exactly like the TypeScript {@linkcode Readonly} utility type.
*
* @example
* <caption>#### Make all properties `readonly` (default behavior)</caption>
* <caption>#### Create a **`readonly`** version of a type</caption>
*
* ```ts
* import { expectTypeOf } from 'expect-type'
Expand All @@ -695,35 +693,11 @@ export interface BaseExpectTypeOf<Actual, Options extends {positive: boolean}> {
* expectTypeOf<Post>().readonly().toEqualTypeOf<Readonly<Post>>()
* ```
*
* @example
* <caption>#### Make specific properties `readonly`</caption>
*
* ```ts
* import { expectTypeOf } from 'expect-type'
*
* type Post = {
* title: string
* content: string
* }
*
* expectTypeOf<Post>()
* .readonly('title')
* .toEqualTypeOf<{ readonly title: string; content: string }>()
* ```
* @returns The type with all properties made **`readonly`**, mirroring the behavior of TypeScript's {@linkcode Readonly} utility.
*
* @param propertiesToMakeReadonly - The specific properties of the {@linkcode Actual} type to be made `readonly`. If omitted, all properties will be made `readonly`, behaving like the TypeScript {@linkcode Readonly} utility type.
* @returns the type with the specified properties made `readonly`. If no properties are specified, all properties will be made `readonly`, behaving like the TypeScript {@linkcode Readonly} utility.
*
* @template PropertiesToMakeReadonly - The keys of the __`Actual`__ type to be made `readonly`. Defaults to `never`, meaning no specific properties are targeted unless explicitly defined.
*
* @since 1.0.0
* @since 1.1.0
*/
readonly<PropertiesToMakeReadonly extends keyof Actual = never>(
propertiesToMakeReadonly?: PropertiesToMakeReadonly,
): ExpectTypeOf<
IsNever<PropertiesToMakeReadonly> extends true ? Readonly<Actual> : SetReadonly<Actual, PropertiesToMakeReadonly>,
Options
>
readonly(): ExpectTypeOf<Readonly<Actual>, Options>

/**
* Extracts a certain function argument with `.parameter(number)` call to
Expand Down
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export type UnionToTuple<Union> = TuplifyUnion<Union>
* expectTypeOf(undefined).not.toMatchTypeOf<AnyNonNullishValue>()
* ```
*
* @since 1.0.0
* @since 1.1.0
* @internal
*/
type AnyNonNullishValue = NonNullable<unknown>
Expand All @@ -267,7 +267,7 @@ type AnyNonNullishValue = NonNullable<unknown>
* const sum = ((a: number, b: number): number => a + b) satisfies AnyFunction
* ```
*
* @since 1.0.0
* @since 1.1.0
* @internal
*/
type AnyFunction = (...args: any[]) => any
Expand Down Expand Up @@ -308,7 +308,7 @@ type AnyFunction = (...args: any[]) => any
* }>()
* ```
*
* @since 1.0.0
* @since 1.1.0
* @internal
* @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/simplify.d.ts | Source}
*/
Expand Down Expand Up @@ -348,7 +348,7 @@ type Simplify<TypeToFlatten> = TypeToFlatten extends AnyFunction
* @template BaseType - The base type whose properties will be transformed to `readonly`.
* @template KeysToBecomeReadonly - The keys of the __`BaseType`__ to be made `readonly`.
*
* @since 1.0.0
* @since 1.1.0
*/
export type SetReadonly<BaseType, KeysToBecomeReadonly extends keyof BaseType> = BaseType extends unknown
? Simplify<Omit<BaseType, KeysToBecomeReadonly> & Readonly<Pick<BaseType, KeysToBecomeReadonly>>>
Expand Down
6 changes: 0 additions & 6 deletions test/usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,6 @@ test('Use `.readonly` to create a `readonly` version of a type', () => {
expectTypeOf<Post>().readonly().toEqualTypeOf<Readonly<Post>>()
})

test('`.readonly` can make specific properties `readonly`', () => {
type Post = {title: string; content: string}

expectTypeOf<Post>().readonly('title').toEqualTypeOf<{readonly title: string; content: string}>()
})

test('Make assertions about object properties', () => {
const obj = {a: 1, b: ''}

Expand Down

0 comments on commit 5a8b2dc

Please sign in to comment.