-
-
Notifications
You must be signed in to change notification settings - Fork 158
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
Implement pick & omit functionality #943
Comments
Is this feature what you want? |
@samchon as I understand clone makes the same as pick. However, I am not sure that it can do omit. |
Clone with typia.misc.assertClone<Omit<T, "a"|"b">>(input); |
@samchon it is not exactly as omit, it doesn't return rest properties. For example
Using ps. looks like clone skips function properties, what also is not the same as
|
If still want this feature, can you write the function interface? |
I think he's talking about something similar: type T1 = {
a: string;
};
type T2 = {
b: string;
};
const obj: T1 & T2 = {
a: "",
b: "",
};
const { target, rest } = fn<T1>(obj);
// target is T1 - { a: "" }
// rest is T2 - { b: "" } This cannot be done now due to TS limitations: Therefore, the prune function (and similar functions) can change the shape of an object without changing its type: |
Feature Request
A description of the problem you're trying to solve.
TypeScript allows you to combine several types into one, however, sometimes you need to extract or omit special subset of fields from that "combined" object. I face the necessity of pick/omit when I work with React because React is unhappy when you pass non-dom properties to jsx, but I think that functions will be useful in other cases too.
An overview of the suggested solution.
Typia could have the following functions:
pick
- takes generic of what fields should be picked and the source object. It returns new object with picked fields from the generic.omit
- takes generic of what fields should be omitted and the source object. It returns new object without fields from the generic.extract
- takes generic of what fields should be extracted and the source object. It returns a tuple with two objects - the extracted fields and the rest fields. As you can see the pick and omit functionality can be achieved via the extract function, but I think to worth to leave all of them just for the purpose of simplicity and performance.Along with other functions from Typia there can be functions like
createPick
,createOmit
andcreateExtract
.Examples of how the suggestion would work in various places.
or
Note about
deep
functionality - probably there will be cases when you want to deeply pick or omit object, but that feels much more complicated and I think it is worth to leave it for further improvements.The text was updated successfully, but these errors were encountered: