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

feat(signals): set state in patchState to readonly #4519

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rainerhahnekamp
Copy link
Contributor

As discussed in #4030, the update function of patchState, provides the state as deep readonly.

Notes on the test: I've decided to go with @ts-expect-error instead of ts-snippet. ts-snippet requires the source code as string, whereas @ts-expect-error can be applied to normal code. This makes it easier to write but also to refactor.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

Closes #4030

What is the new behavior?

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Copy link

netlify bot commented Sep 7, 2024

Deploy Preview for ngrx-io canceled.

Built without sensitive environment variables

Name Link
🔨 Latest commit fb02f54
🔍 Latest deploy log https://app.netlify.com/sites/ngrx-io/deploys/66dccb7b492a630008ec9569

@@ -27,3 +27,11 @@ export type IsKnownRecord<T> = IsRecord<T> extends true
export type OmitPrivate<T> = {
[K in keyof T as K extends `_${string}` ? never : K]: T[K];
};

export type DeepReadonly<T> = T extends object
Copy link
Contributor

Choose a reason for hiding this comment

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

The type-fest library has a more comprehensive version of this: https://github.com/sindresorhus/type-fest/blob/main/source/readonly-deep.d.ts. I can't tell the differences off the top of my head but thought I'd mention it.

(Note: I'm not advocating using the type-fest library as a dependency — more providing this in case of inspiration for some good 'ol copy-pasting :))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @jits, I took a look at it before. It supports a lot of types that are not allowed to be part of the state object. So I took arrays, primitive types and nested objects out of it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @rainerhahnekamp — ahh, perfect! Thanks for clarifying. (And that makes a lot of sense)

@markostanimirovic
Copy link
Member

I suggest keeping types as they are and displaying a warning in dev mode if the state is mutated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@ngrx/signals: Improve Developer Experience for Immutability
3 participants