Skip to content

Commit

Permalink
Use overloads for .readonly()
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaemami59 committed Sep 14, 2024
1 parent 88f3563 commit 2b28aab
Showing 1 changed file with 46 additions and 14 deletions.
60 changes: 46 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@ import {
OverloadReturnTypes,
OverloadsNarrowedByParameters,
} from './overloads'
import type {
StrictEqualUsingTSInternalIdenticalToOperator,
AValue,
MismatchArgs,
Extends,
IsNever,
SetReadonly,
} from './utils'
import type {StrictEqualUsingTSInternalIdenticalToOperator, AValue, MismatchArgs, Extends, SetReadonly} from './utils'

export * from './branding' // backcompat, consider removing in next major version
export * from './utils' // backcompat, consider removing in next major version
Expand Down Expand Up @@ -704,12 +697,51 @@ export interface BaseExpectTypeOf<Actual, Options extends {positive: boolean}> {
*
* @since 1.0.0
*/
readonly<PropertiesToMakeReadonly extends keyof Actual = never>(
propertiesToMakeReadonly?: PropertiesToMakeReadonly,
): ExpectTypeOf<
IsNever<PropertiesToMakeReadonly> extends true ? Readonly<Actual> : SetReadonly<Actual, PropertiesToMakeReadonly>,
Options
>
readonly<PropertiesToMakeReadonly extends keyof Actual>(
propertiesToMakeReadonly: PropertiesToMakeReadonly,
): ExpectTypeOf<SetReadonly<Actual, PropertiesToMakeReadonly>, 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.
*
* @example
* <caption>#### Make all properties `readonly` (default behavior)</caption>
*
* ```ts
* import { expectTypeOf } from 'expect-type'
*
* type Post = {
* title: string
* content: string
* }
*
* 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 the specified properties made `readonly`. If no properties are specified, all properties will be made `readonly`, behaving like the TypeScript {@linkcode Readonly} utility.
*
* @since 1.0.0
*/
readonly(): ExpectTypeOf<Readonly<Actual>, Options>

/**
* Extracts a certain function argument with `.parameter(number)` call to
Expand Down

0 comments on commit 2b28aab

Please sign in to comment.