This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// typescript version is 4.6.3 | |
// type CamelCase<S extends string> = S extends `${infer P1}_${infer P2}${infer P3}` | |
// ? `${Lowercase<P1>}${Uppercase<P2>}${CamelCase<P3>}` | |
// : Lowercase<S> | |
// type CamelCase<S extends string> = S extends `${infer T}_${infer U}` ? `${T}${Capitalize<CamelCase<U>>}` : S; | |
// import {CamelCase} from 'type-fest' | |
// Opaque reference from: https://blog.beraliv.dev/2021-05-07-opaque-type-in-typescript |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useRef, useEffect } from "react"; | |
function useWhyDidYouUpdate(name, props) { | |
// Get a mutable ref object where we can store props ... | |
// ... for comparison next time this hook runs. | |
const previousProps = useRef(); | |
useEffect(() => { | |
if (previousProps.current) { | |
// Get all keys from previous and current props | |
const allKeys = Object.keys({ ...previousProps.current, ...props }); | |
// Use this object to keep track of changed props |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const doorMachinceConfig = { | |
id: "door", | |
initial: "關著", | |
states: { | |
關著: { | |
on: { 開門: "開了" } | |
}, | |
開了: { | |
on: { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const inputMachine = Machine({ | |
id: "inputMachine_v2", | |
type: "parallel", | |
states: { | |
enabled: { | |
initial: "enabled", | |
states: { | |
disabled: { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const inputMachine = Machine({ | |
id: "inputMachine_v1", | |
type: "parallel", | |
states: { | |
valid: { | |
initial: "valid", | |
states: { | |
invalid: { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const parallelMachine = Machine({ | |
id: "myParallelMachineId", | |
type: "parallel", | |
states: { | |
state1: { | |
initial: "state1-1", | |
states: { | |
"state1-1": { | |
on: { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createMachine } from 'xstate'; | |
const machineConfig = { | |
id: 'news-version-1', | |
initial: "draft", | |
states: { | |
draft: { | |
on: { | |
REVIEW: { | |
target: "reviewing", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const doorMachine = Machine({ | |
// Machine identifier | |
id: "door", | |
// Initial state | |
initial: "關著", | |
// State definitions | |
states: { | |
關著: { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Simplify<T> = T extends Function | |
? T | |
: T extends Record<string, any> | |
? { [K in keyof T]: Simplify<T[K]> } | |
: T; | |
type Author = { | |
lastName: string; | |
firstName: string; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Example<T> = T extends Record<string, any> ? keyof T : T; | |
// type T1 = number | keyof string[] | |
type T1 = Example<string[]>; | |
// type T2 = "a" | "b" | |
type T2 = Example<{ a: number; b: string }>; | |
// type T3 = boolean | |
type T3 = Example<boolean>; |
NewerOlder