Skip to content

Instantly share code, notes, and snippets.

View disintegrator's full-sized avatar

Georges Haidar disintegrator

View GitHub Profile
@disintegrator
disintegrator / Value.ts
Last active April 26, 2018 12:23
Value component
import * as React from "react";
interface Props<T> {
initial?: T;
children: (r: { value?: T; onChange: (value: T) => void }) => React.ReactNode;
}
interface State<T> {
value?: T;
}
@disintegrator
disintegrator / PropType.tsx
Created May 1, 2018 22:46
PropType conditional type inference in TypeScript
import { ComponentType } from "react";
/**
* For any type T that is a React.ComponentType give me the type of its Props
*
*/
export type PropType<T> = T extends ComponentType<infer P> ? P : never;
// USAGE:
import { Component, Fragment } from "react";
@disintegrator
disintegrator / propTypes.ts
Created September 5, 2018 22:29
Get the prop types of a react component class, functional component or string corresponding to HTML tag name
import * as React from "react";
/**
* Given a JSX.IntrinsicElement[...], get its attributes
*/
type Attributes<E> = E extends React.DetailedHTMLProps<infer Attr, any>
? Attr
: never;
/**
@disintegrator
disintegrator / https-during-dev.macos.sh
Last active April 23, 2024 22:41
Use Caddy, mkcert and dnsmasq to expose your development server over HTTPS
brew install caddy mkcert nss dnsmasq
mkcert -install
mkcert '*.app.test' '*.cdn.test'
# rename the certs and move them under /usr/local/etc/caddy/certs
cat <<EOF > /usr/local/etc/caddy/Caddyfile
*.app.test:443, *.cdn.test:443 {
@disintegrator
disintegrator / TypeHolesInTS.md
Last active November 3, 2021 02:45
Type holes in TypeScript

Type holes in TypeScript

In TypeScript there are a few ways you can sidestep type inference and the compiler to introduce type holes in your code. Type holes are parts of the code where we’ve lied or hid information from the compiler and can be a source of bugs that are not present in properly typed code. The common ones I see are listed below in order of really bad to bad.

Type assertions

const something = getValue() as any;
const cat = dog as Cat;
@disintegrator
disintegrator / GatsbyContentfulWithReadingTime.graphql
Last active June 19, 2022 13:43
Add a reading time estimate to all your Contentful rich text nodes in GatsbyJS
{
contentfulBlogPost {
body {
fields {
readingTime {
text
minutes
time
words
}
@disintegrator
disintegrator / pino.d.ts
Created September 24, 2020 12:53
An example TypeScript declaration file that demonstrates how to augment the type definitions of a third party library.
import "pino";
declare module "pino" {
interface LoggerOptions {
// @types/pino does not currently expose this option
nestedKey?: string;
}
}
@disintegrator
disintegrator / example_1.html
Created December 11, 2020 15:11
Having Fun with Markdown and Remark
<h1>I am a level 1 heading</h1>
<p class="subtitle subtitle--1">I am a level 1 subtitle</p>
<p class="subtitle subtitle--2">I am a level 2 subtitle</p>
<p class="subtitle subtitle--6">
Up to six levels are supported corresponding to heading levels
</p>
@disintegrator
disintegrator / typescriptreact.json
Created December 18, 2020 10:11
VS Code snippets
{
"React function component": {
"prefix": "nrf",
"body": [
"import * as React from \"react\";",
"",
"export interface ${1:$TM_FILENAME_BASE}Props {}",
"",
"const ${1:$TM_FILENAME_BASE}: React.FC<${1:$TM_FILENAME_BASE}Props> = props => {",
" return <>{props.children}</>;",
@disintegrator
disintegrator / debounce.go
Created December 23, 2021 18:52
A debouncer that sends pulses on a channel (Golang, Go)
package debounce
import (
"context"
"errors"
"fmt"
"sync"
"time"
)