You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
interface LayoutStyles {
display?: 'block' | 'flex'
width?: number
height?: number
// many other fields
}
interface SpacingStyles {
padding?: string
margin?: string
// many other fields
}
interface Props extends LayoutStyles, SpacingStyles, ButtonHTMLAttributes<HTMLButtonElement> {}
function Component(props: PropsWithChildren<Props>) {
return (
// Along with button html props we pass some attributes that doesn't exist in html lang
// Moreover, we accidentally pass width and height attributes which exist in html
// and hence they will be added in the final markup, but it is not what we want
<button
{...props}
style={{ display: props.display, width: props.width, height: props.height, padding: props.padding, margin: props.margin }}
>
{props.children}
</button>
)
}
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 and createExtract.
Examples of how the suggestion would work in various places.
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:
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: