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

Make applying DeepReadonly idempotent #107

Merged
merged 4 commits into from
Oct 4, 2019

Conversation

aaronjensen
Copy link
Contributor

fixes #106

This isn't as clean as I'd like—ideally a recursive application would have no impact. For example, the newly added test would match as if it weren't applied twice:

exports[`DeepReadonly testType<DeepReadonly<DeepReadonly<NestedArrayProps>>['first']['second']>() 1`] = `"_DeepReadonlyArray<_DeepReadonlyObject<{ name: string; }>>"`;

Above, the _DeepReadonlyObject is superfluous and isn't there if applied only once.

exports[`DeepReadonly testType<DeepReadonly<NestedArrayProps>['first']['second']>() 1`] = `"_DeepReadonlyArray<{ name: string; }>"`;

Description

Related issues:

Checklist

  • I have read CONTRIBUTING.md
  • I have linked all related issues above
  • I have rebased my branch

For bugfixes:

  • I have added at least one unit test to confirm the bug have been fixed
  • I have checked and updated TOC and API Docs when necessary

For new features:

  • I have added entry in TOC and API Docs
  • I have added a short example in API Docs to demonstrate new usage
  • I have added type unit tests with dts-jest

@aaronjensen
Copy link
Contributor Author

Actually, it turns out that the issue I pointed to above is a real problem, those are two different types according to typescript... Any ideas on how to detect that we've already applied DeepReadonly and short-circuit?

@steida
Copy link

steida commented Oct 1, 2019

Maybe this will help

(More) Recursive Type Aliases
https://devblogs.microsoft.com/typescript/announcing-typescript-3-7-beta/

@aaronjensen
Copy link
Contributor Author

Okay, I've pushed up a commit with a new solution that uses a symbol tag to skip applying DeepReadonly again. I'm not sure how you feel about privateish symbol tags, but this is the only way I know how to do something like this.

@aaronjensen
Copy link
Contributor Author

@steida I think that'll help with the ultimate definition so we don't have to create separate interfaces to enable recursion, but I'm not sure it helps with this directly, but maybe I'm missing something.

@aaronjensen aaronjensen changed the title Allow recursively applying DeepReadonly Make applying DeepReadonly idempotent Oct 1, 2019
@aaronjensen
Copy link
Contributor Author

Unfortunately, the tagging taints Records, which makes things like Object.entries not work like you'd want it to. I'm currently at a loss on how to express this properly and make it idempotent.

@aaronjensen
Copy link
Contributor Author

Okay, 24d4555 seems to work like I want it to so far. The only downside is it strips the names of types that are made ReadOnly (see here).

src/mapped-types.ts Show resolved Hide resolved
Copy link
Owner

@piotrwitek piotrwitek left a comment

Choose a reason for hiding this comment

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

I have added new comments, please ask if you have any doubts.

src/mapped-types.ts Outdated Show resolved Hide resolved
src/mapped-types.ts Outdated Show resolved Hide resolved
src/mapped-types.spec.ts Outdated Show resolved Hide resolved
src/mapped-types.spec.ts Outdated Show resolved Hide resolved
@aaronjensen
Copy link
Contributor Author

Addressed feedback in 9fa8988, I think there may be a conflict between the dts snap and prettier now, however—not sure how you want to resolve that. I could split up the primitive test so prettier doesn't want to reformat. ci-check does pass, so maybe it's fine.

@aaronjensen
Copy link
Contributor Author

Also, let me know if you'd like me to squash the commits or if you just plan on doing it w/ GitHub. Thanks!

Copy link
Owner

@piotrwitek piotrwitek left a comment

Choose a reason for hiding this comment

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

Thanks, looks neat!

Comment on lines +345 to +354

// @dts-jest:pass:snap -> _DeepReadonlyObject<{ first: { second: { name: string; }; }; }>
testType<DeepReadonly<DeepReadonly<NestedProps>>>();
// @dts-jest:pass:snap -> _DeepReadonlyObject<{ first: { second: { name: string; }[]; }; }>
testType<DeepReadonly<DeepReadonly<NestedArrayProps>>>();

// @dts-jest:pass:snap -> string | number | bigint | boolean | symbol | null | undefined
testType<
DeepReadonly<string | null | undefined | boolean | number | bigint | symbol>
>();
Copy link
Owner

Choose a reason for hiding this comment

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

Looks good 👍

: T extends _DeepReadonlyArray<infer U>
? _DeepReadonlyArray<U>
: T extends _DeepReadonlyObject<infer V>
? _DeepReadonlyObject<V>
Copy link
Owner

Choose a reason for hiding this comment

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

Looks good!

@piotrwitek piotrwitek merged commit 13387ab into piotrwitek:master Oct 4, 2019
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.

Allow DeepReadonly to be applied more than once
3 participants