This file contains hidden or 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 Big = { x: string } | |
type Small = Big & { y: string } | |
// 잘못된 다형적 컴포넌트 | |
interface Props { | |
value: Big // Small도 Big이니까 넣어도 됨 | |
onReport(newValue: Big): void // 이 코드의 의도는 (newValue: Small) => void도 전달받는 걸 상정함 | |
} | |
function WrongPolymorphicComponent({ value, onChange }: Props) { |
This file contains hidden or 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
/** | |
* Inject all key values written in userOptions | |
* If a key value doesn't exist, use the value from defaultOptions | |
* In this case, even if userOptions[key] is undefined, it overwrites if given | |
* If you want behavior identical to Object.assign, set `isUndefinedIgnored` to `true` | |
* | |
* @param userOptions 사용자가 입력할 부분적인 값 | |
* @param defaultOptions 기본적으로 제공할 값 | |
* @param isUndefinedIgnored undefined 값을 무시할 지 여부 | |
* @returns |
This file contains hidden or 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 { useLayoutEffect, useRef, useState } from 'react' | |
export function usePromise<T>(promise: Promise<T>) { | |
const [isLoading, setIsLoading] = useState(true) | |
const [value, setValue] = useState<T>() | |
const valueVersion = useRef(0) | |
useLayoutEffect(() => { | |
const valueOnClosure = ++valueVersion.current |
This file contains hidden or 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 { useEffect } from 'react'; | |
type UseAsyncEffectCleaner = () => Promise<void> | void; | |
type UseAsyncEffectCallback = () => | |
| Promise<UseAsyncEffectCleaner | void> | |
| UseAsyncEffectCleaner | |
| void; | |
export function useAsyncEffect(fn: UseAsyncEffectCallback, deps?: any[]) { | |
return useEffect(() => { |
This file contains hidden or 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
interface TreeNode<T> { | |
value: T | |
children?: TreeNode<T>[] | |
} | |
function preTraverse<T>(node: TreeNode<T>): IterableIterator<T> { | |
const stack = [{ node, index: -1 }] | |
return { | |
[Symbol.iterator]() { |
This file contains hidden or 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
export type DeepKeys<T, K = keyof T, R extends string[] = []> = | |
K extends keyof T ? | |
T[K] extends object ? | |
T[K] extends unknown[] | [unknown, ...unknown[]] ? | |
[...R, K] | [...R, K, KeyOfArrayLike<T[K]>] | |
: [...R, K] | [...R, K, ...DeepKeys<T[K], keyof T[K], R>] | |
: [...R, K] | |
: [] | |
type KeyOfArrayLike<T> = |
This file contains hidden or 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 KeyOfArrayLike<T> = | |
T extends [] ? | |
never | |
: T extends [unknown, ...infer R] ? | |
R['length'] | KeyOfArrayLike<R> | |
: T extends unknown[] ? | |
number | |
: never |
This file contains hidden or 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
export type Tree<T, K> = T & { | |
id: K | |
children: Tree<T, K>[] | |
} | |
// Based on KMP algorithm | |
export function matchTree<T, K>( | |
root: Tree<T>, | |
prop: (value: Tree<T>) => K, | |
selector: K[], |
This file contains hidden or 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 Remove<R extends unknown[], I extends number, Out extends unknown[] = [], Count extends unknown[] = []> | |
= R extends [] | |
? Out | |
: R extends [infer First, ...infer Rest] | |
? Count['length'] extends I | |
? Remove<Rest, I, Out, [...Count, 0]> | |
: Remove<Rest, I, [...Out, First], [...Count, 0]> | |
: never | |
type X = Remove<['a', 'b', 'c'], 1> |
NewerOlder