Skip to content

Instantly share code, notes, and snippets.

@misha-erm
misha-erm / KeyBy.ts
Last active May 20, 2021 10:06
How to key ReadonlyArray type by 'id' key?
/*
*
* I want to key array by 'id'
*
* FROM: [{id: 'a', name: 'A'}, {id: 'b', name: 'B'}]
* TO: { a: {id: 'a', name: 'A'}, b: {id: 'b', name: 'B'} }
*
*/
const arr = [{id: 'a', name: 'A'}, {id: 'b', name: 'B'}] as const
@misha-erm
misha-erm / cif.ts
Last active March 29, 2021 10:47
[Typescript] - Constrained identity function
/**
* Problem: defining object with interfaces grant autocomplete but also widen all types (e.g. `true` becomes `boolean`)
* Defining object with `as const` infers all the narrowed types but looses autocomplete
* Solution: Create utility function with constrained generic type parameters. More info [here](https://kentcdodds.com/blog/how-to-write-a-constrained-identity-function-in-typescript)
*/
interface ISchema {
id: string;
description: string;
@misha-erm
misha-erm / Dockerfile
Created January 4, 2021 12:10
Dockerfile for node.js + typescript + yarn with layer caching
FROM node:14.15-alpine as base
WORKDIR /home/app
COPY package.json yarn.lock ./
# ---prepare dependencies---
FROM base as dependencies
# install production dependencies
RUN yarn install --production --frozen-lockfile
RUN mv node_modules prod_node_modules
@misha-erm
misha-erm / determine-changed-props.js
Created October 3, 2019 15:30 — forked from sorenlouv/determine-changed-props.js
Determine which props causes React components to re-render
import React from 'react';
export default function withPropsChecker(WrappedComponent) {
return class PropsChecker extends React.Component {
componentWillReceiveProps(nextProps) {
Object.keys(nextProps)
.filter(key => {
return nextProps[key] !== this.props[key];
})
.map(key => {