Open aspirisen opened 7 months ago
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 Omit<T, K>
type like below may what you want.
typia.misc.assertClone<Omit<T, "a"|"b">>(input);
@samchon it is not exactly as omit, it doesn't return rest properties.
For example
interface Test {
a?: string;
b?: string
}
const data: Record<string, any> = { c: 'a' }
const other = typia.misc.omit<Test>(data)
Using typia.misc.assertClone<Omit<Test, keyof Test>>(data);
looks like does nothing
ps. looks like clone skips function properties, what also is not the same as pick
const $co0 = (input)=>({
onPress: "function" === typeof input.onPress ? undefined : input.onPress
});
return "object" === typeof input && null !== input ? $co0(input) : input;
If still want this feature, can you write the function interface?
I think he's talking about something similar: https://github.com/samchon/typia/issues/202 https://github.com/lodash/lodash/issues/3172
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: https://stackoverflow.com/questions/76490186/use-type-parameters-with-exact-type-instead-of-extending
Therefore, the prune function (and similar functions) can change the shape of an object without changing its type: playground (https://github.com/samchon/typia/issues/1205)
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.