Spread operator in Typescript
The spread operator combines a powerful operator with a simplistic syntax. While this is not a new operator in Typescript, I find it can be useful to take a quick refresher on the possibilities of using this feature.
Get Dave Ahern’s stories in your inbox
Join Medium for free to get updates from this writer.
If you are also looking for a quick and easy refresher, please try the quiz below. We will present you with several questions each containing an answer. Challenge yourself to guess the output without peeking on the answers.
Question 1
let user = {username: 'user1', password: 'password'};
let newUser = {...user, phone: '0864234544'};
console.log(newUser);{ username: 'user1', password: 'password', phone: '0864234544' }
// Its simple to merge 2 objectsQuestion 2
let user = {username: 'user1', password: 'password'};
let newUser = {...user, username: 'jimmy'};
console.log(newUser);{ username: 'jimmy', password: 'password' }
// The right most key takes precedenceQuestion 3
let user = {username: 'user1', password: 'password'};
let info = {number: '087 63455424', address:'Cork, Ireland'};
let result = {...user, ...info};
console.log(result);{ username: 'user1', password: 'password', number: '087 63455424', address: 'Cork, Ireland' }
// You can easily spread multiple objectsQuestion 4
let user = {username: 'user1', address: {line1: 'Cork', line2: 'Ireland'}};
let newUser = {...user, address: {line2: 'Jamaica'}};
console.log(newUser);{ username: 'user1', address: { line2: 'Jamaica' } }
// If your wondering where line1 went from the address don't worry, this is a common gotcha.
// If we wanted to keep line1 of our address then we need to spread this also using the example below
let user = {username: 'user1', address: {line1: 'Cork', line2: 'Ireland'}};
let newUser = {...user, address: {...user.address, line2: 'Jamaica'}};
console.log(newUser);
{ username: 'user1', address: { line1: 'Cork', line2: 'Jamaica' } }}Question 5
let users = {
user1: {
username: 'timmy'
},
user2: {
username: 'jimmy'
},
user3: {
username: 'tommy'
}
};
let result = {...users, ['user2']: {...users.user2, password: 'jimmypass'}};
console.log(result);{ user1: { username: 'timmy' }, user2: { username: 'jimmy', password: 'jimmypass' }, user3: { username: 'tommy' } }
// We simply gave jimmy a password. This is a good example of how you update a user based on their user keyQuestion 6
let users = ['user1', 'user2'];
let newUsers = ['user3', 'user4'];
let allUsers = [...users, ...newUsers];
console.log(allUsers);['user1', 'user2', 'user3', 'user4']
// This demonstrates that we can also spread arraysQuestion 7
function saySomething(...args: string[]) {
console.log(args.join(' '));
}
saySomething('spread', 'operators', 'are', 'fun');"spread operators are fun"
// Function arguments can also be spreadConclusion
I hope this gives quick quiz has helped you out or given you a sense of how powerful this operator is. It can be useful in many scenarios. I use it mainly when working with redux libraries such as NGRX.








