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

Introduce the .readonly() method #117

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

aryaemami59
Copy link
Collaborator

This PR:

  • Introduces the .readonly() method for transforming object properties to readonly.

Details:

.readonly() works similarly to the native Readonly utility type.

Default Behavior:

When no arguments are passed to .readonly(), it behaves like the Readonly utility, converting all properties of the target type to readonly. For example:

import { expectTypeOf } from 'expect-type'

type Post = {
  title: string
  content: string
}

expectTypeOf<Post>().readonly().toEqualTypeOf<Readonly<Post>>()

Selective Readonly Properties:

You can also specify particular properties to make readonly by passing the keys of the target object as parameters. Only those specified properties will become readonly while others remain mutable. For instance:

import { expectTypeOf } from 'expect-type'

type Post = {
  title: string
  content: string
}

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

@aryaemami59 aryaemami59 added the New feature New feature or request label Sep 12, 2024
@aryaemami59 aryaemami59 added this to the 1.0.0 milestone Sep 12, 2024
@aryaemami59 aryaemami59 self-assigned this Sep 12, 2024
Copy link

pkg-pr-new bot commented Sep 12, 2024

Open in Stackblitz

pnpm add https://pkg.pr.new/mmkal/expect-type@117

commit: 2b28aab

@aryaemami59 aryaemami59 force-pushed the add-readonly branch 3 times, most recently from b35844b to c16abc2 Compare September 14, 2024 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant