Skip to content

Instantly share code, notes, and snippets.

@iohcidnal
iohcidnal / dynamic-fn-signature.ts
Last active June 24, 2022 15:55
Create dynamic function signature
type Person =
| {
firstName: string;
lastName: string;
isMarried: false;
}
| {
isMarried: true;
spouse: {
spouseFirstName: string;
@iohcidnal
iohcidnal / create-intersection-type.ts
Created June 23, 2022 01:47
Create a new intersection type from an existing type
type Foo = {
prop1: {
value: string;
};
prop2: {
value: string[];
};
prop3: {
value: number;
};
@iohcidnal
iohcidnal / concurrent-awaits.js
Created July 5, 2018 02:16
Concurrently dispatches promises
(async () => {
const promises = [
new Promise(resolve => resolve(1)),
new Promise(resolve => resolve(2)),
new Promise(resolve => resolve(3)),
new Promise(resolve => resolve(4)),
new Promise(resolve => resolve(5)),
];
const [...result ] = await Promise.all(promises.map(async p => {
@iohcidnal
iohcidnal / delete-item-from-array.js
Created May 20, 2018 21:44
delete-item-from-array
const arr = [
'a', 'b', 'c', 'd', 'e'
];
const index = 2; // remove 'c'
const newarr = [
...arr.slice(0, index), // [ 'a', 'b' ]
...arr.slice(index + 1), // [ 'd', 'e' ]
];
@iohcidnal
iohcidnal / update-array.js
Last active May 12, 2018 20:50
Updating element in array
const arr = [
{ id: 0, name: 'john'},
{ id: 1, name: 'mark' },
{ id: 2, name: 'luke' },
{ id: 3, name: 'peter' },
];
const newPerson = {id: 2, name: 'newPerson'};
const index = arr.findIndex(val => val.id === newPerson.id);
console.log(index);
@iohcidnal
iohcidnal / update-nested-object.js
Last active May 5, 2018 21:08
Updating nested object
const a = {
id: 1,
name: {
lastName: '',
}
};
const n = 'name';
const b = {