slice
slice
Accepts an object of type T and a single key or an array of keys
(K extends keyof T). Constructs a new object from the provided keys without
mutating the original object.
Immutability is explained in the immutability & serializable state concept.
Example
const cat = { id: 1, type: 'cat', name: 'Fluffy' };
const catWithoutType = slice(cat, ['name', 'id']);
// catWithoutType will be:
// {id: 1, name: 'Fluffy'};
Example — with rxState()
import { Component, inject } from '@angular/core';
import { rxState } from '@rx-angular/state';
import { slice } from '@rx-angular/cdk/transformations';
@Component({
/* ... */
})
export class AnimalsListComponent {
private readonly api = inject(ApiService);
private readonly state = rxState<ComponentState>(({ connect }) => {
connect('animals', this.api.getAnimals(), (_, animals) => animals.map((animal) => slice(animal, ['id', 'name'])));
});
readonly animals = this.state.signal('animals');
}
Example — signals-first
import { signal } from '@angular/core';
import { slice } from '@rx-angular/cdk/transformations';
const cat = signal<Cat>({ id: 1, type: 'cat', name: 'Fluffy' });
// no native object-pick equivalent — slice keeps its role
const catWithoutType = computed(() => slice(cat(), ['id', 'name']));
Edge cases
slice(nonObject, 'prop' as any) > undefined;
slice(null as any, 'prop') > undefined;
slice(null as any, null as any) > undefined;
slice([state], 'concat') > undefined;
slice(state, 'nonExisting' as any) > undefined;
slice(state, null as any) > undefined;
slice(state, ['stateProp1', 'nonExistingProp']) > { stateProp1: stateProp1Value };
Signature
function slice<T extends object, K extends keyof T>(object: T, keys: K | K[]): Pick<T, K>;