There's plenty of info online on using TypeScript's Pick<T> and Omit<T> to construct types that only have a subset of attributes of some class.
What I've completely failed to find is how to leverage the newly created type to initialize an object of said type from attributes of the initial class.
Consider the following:
class Foo {
a: string;
b: number;
c: string;
}
type FooStrings = Pick<Foo, 'a' | 'c'>;
function getFoos(): Array<Foo> {
// create/get and return a bunch of Foo objects
}
function parseFoos(foos: Array<Foo>): Array<FooStrings> {
// ???
}
What exactly am I supposed to put into parseFoos to do only get FooStrings objects without going
source: Foo;
parsed: FooStrings = {
a: source.a,
c: source.c
}
every time? Object.assign seems to be more than happy to add all the missing properties.
Foois already, structurally, usable everywhere aFooStringsis, soparseFooscan justreturn foosand satisfy the type checking.