interface Config {
host?: string;
port?: number;
user?: string;
password?: string | null;
}
const config: Config = {
host: 'localhost',
port: undefined,
user: 'admin',
password: null
};
const definedConfig = keepDefined(config, 'host', 'port', 'user', 'password');
// Result: { host: 'localhost', user: 'admin' }TObject
the original object type you're pulling from
TObjectthe original object type you're pulling from
TKey
the keys to copy when defined
TKeythe keys to copy when defined
source
source: TObjectthe object to read values from
keys
keys: readonly TKey[]optional list of keys to include when present.
Default:all keys