43
loading...
This website collects cookies to deliver better user experience
Pick
ed object - an object with the properties we want "picked" out of the original interface.type
as well as for the object.const buildImplicitPickVer1 =
<T>() =>
<K extends keyof T>(props: Pick<T, K>): Pick<T, K> =>
props;
// Usage
const pickProduct = buildImplicitPickVer1<Product>();
const implicitProduct = pickProduct({ id: '1', ... })
<T>() =>
<K extends keyof T>
K
that is constrained to the type given in the factory.(props: Pick<T, K>): Pick<T, K>
Pick
ed object values.Pick<T, K>
relies on keys given. Lets fix that!const buildImplicitPick =
<T>() =>
<K extends keyof T>(props: Partial<T> & Pick<T, K>): Pick<T, K> =>
props;
Partial<T> & Pick<T, K>
Partial<T>
give us the ability to get back our auto complete for keys.& Pick<T, K>
ensures that we get the correct type for our key.type A = { a: number | undefined }
type B = { a: number }
type C = A & B; // will be { a: number } since that is what both types above have.
CTRL + SPACE
to see what props are available, they are all optional because of the Partial
.K
keys are updated;Pick<T, K>
Partial<T> & Pick<T, K>
enforces are type to be required.