I have this function
function someThing(someArg: Pick<HTMLElement, 'id' | 'style'>) {...}
However, I only want to add some styles to someArg.style
, not all the style from CSSStyleDeclaration
. Otherwise, the input of the function will be huge.
So I expect to have something like Pick
from Partial<CSSStyleDeclaration>
not Pick
from CSSStyleDeclaration
How can I do that?
Since your type is more complicated than just
Pick
ing some properties from an existing type, you might want to spell it out in its own interface:I'm using lookup types to make it clear that the
id
andstyle
properties are related to those fromHTMLElement
, but you could use juststring
andPartial<CSSStyleDeclaration>
instead.If you really need to use something like
Pick
that allows subproperties to bePartial
you can make a mapped type to do it, but I can't tell from your question if you need something programmatic.UPDATE
I guess you do want it to be programmatic (although for just two properties I wouldn't bother, myself) so here's a way to define
PickPartial
so that you are picking fromPartial
properties instead of the properties themselves. I assume you don't need it to be recursive (you don't need each of the subproperties to themselves bePartial
):Note that while this technically makes
id
the typePartial<string>
, that is fine because mapped types do not modify primitives..Partial<string>
is juststring
. So the type ofsomeArg
is the same as theSomeArg
interface I spelled out above.Hope that helps. Good luck!