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

Use key remapping from Typescript 4.1 for PickProperties and OmitProperties #395

Merged

Conversation

DanielBertocci
Copy link
Contributor

PR Checklist

  • Addresses an existing open issue: related to #000
  • Steps in Contributing were taken

Overview

I took advantage of key remapping feature introduced starting from Typescript 4.1.

This allows to reduce the number of iterations when these types are used and to remove the utility PickKeysByValue.

In tests, the "wtf" cases continue to be a bit "wtf" but they are closer to be keys, at least.

I wanted to use this syntax for other cases, but it changes the behavior a bit more and, despite I think it would be reasonable the new behavior, it requires more discussion.

@Beraliv
Copy link
Collaborator

Beraliv commented May 19, 2024

@DanielBertocci Hey again! 👋

I really appreciate you took your time to improve some of the types. As the current ts-essentials version requires typescript>=4.5, I'm happy to apply these improvements.

But before merging the changes, let me quickly analyse what's the performance gain (the diff between the "before" and the "after" in terms of instantiations) because I think it's a significant improvement (FYI I'm doing benchmarks in this PR - #393)

import { PickKeysByValue } from "../pick-keys-by-value";

export type OmitProperties<Type, Value> = Omit<Type, PickKeysByValue<Type, Value>>;
export type OmitProperties<Type, Value> = { [Key in keyof Type as Type[Key] extends Value ? never : Key]: Type[Key] };
Copy link
Collaborator

Choose a reason for hiding this comment

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

Amazing work! It's very performant right now

Screenshot 2024-05-19 at 12 20 49

@Beraliv Beraliv mentioned this pull request May 19, 2024
2 tasks
@@ -1,3 +1,3 @@
import { PickProperties } from "../pick-properties";

export type PickKeys<Type, Value> = Exclude<keyof PickProperties<Type, Value>, undefined>;
export type PickKeys<Type, Value> = keyof PickProperties<Type, Value>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ha, it's amazing that key remapping fixed the problem with undefined

Copy link
Collaborator

@Beraliv Beraliv left a comment

Choose a reason for hiding this comment

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

@DanielBertocci LGTM! I don't see any regressions as all tests are passed. I will update the instantiations and create changelog in a separate PR so I'm happy for this PR to be merged now

@DanielBertocci
Copy link
Contributor Author

Great! Just as remark that this PR introduces a breaking change on the edge cases:

  • PickKeys<boolean, number | undefined> now maps to "valueOf"
  • PickKeys<symbol, number | undefined> now maps to SymbolConstructor["toPrimitive"] | SymbolConstructor["toStringTag"] | "toString" | "valueOf"

@Beraliv
Copy link
Collaborator

Beraliv commented May 19, 2024

@DanielBertocci I am ready to accept the risk that this is a very unrealistic edge case therefore I will leave it as a "patch" anyway. The majority of the cases should apply this type to the object.

@Beraliv Beraliv merged commit 0ababca into ts-essentials:master May 19, 2024
13 checks passed
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.

None yet

2 participants