随笔分类 - TypeScript
摘要:import {Expect, Equal} from "../types/utils" import { expect, it } from 'vitest'; type PayloadsToDiscriminatedUnion<T extends Record<string, any>> = {
阅读全文
摘要:interface EnemyAttacker { fireWeapon(): void; driveForward(): void; assignDriver(driverName: string): void; } // Target class EnemyTank implements Ene
阅读全文
摘要:Example code: interface Fruit { name: string; price: number; } export const wrapFruit = <TFruits extends Fruit[]>(fruits: TFruits) => { const getFruit
阅读全文
摘要:Original code: import { CSSProperties } from "react"; const useStyled = <TTheme = {}>(func: (theme: TTheme) => CSSProperties) => { // Imagine that thi
阅读全文
摘要:This the following code example, test have passed for both run time and compile time: import { expect, it } from 'vitest'; import { Equal, Expect } fr
阅读全文
摘要:export const inferItemLiteral = <T>(t: T) => { return { output: t, }; }; const result1 = inferItemLiteral("a"); // ? {output: string} /* vs */ export
阅读全文
摘要:There are two options doing the same things. const makeSafe = <TParams extends any[], TReturn>(func: (...args: TParams) => TReturn) => ( ...args: TPar
阅读全文
摘要:The transpose of a matrix is an operator which flips a matrix over its diagonal; that is, it switches the row and column indices of the matrix A by pr
阅读全文
摘要:const demoFunc = <TObj extends {}>(obj: TObj, key: ErrorIfNever<keyof TObj, `You much pass at least one key`>) => { return obj[key as keyof TObj] } ty
阅读全文
摘要:type TMethodListener<T> = (copyFn: T, ...args: any[]) => void; type TBuildInfo<TOverriden> = { mutationList: TOverriden; error?: string; }; /** * @des
阅读全文
摘要:import express, { RequestHandler } from 'express'; import { it } from 'vitest'; import { z, ZodError } from 'zod'; import { Equal, Expect } from '../h
阅读全文
摘要:import { expect, it } from 'vitest'; import { Equal, Expect } from '../helpers/type-utils'; export function compose<T1, T2>(func: (t1: T1) => T2): (t1
阅读全文
摘要:const pick = <TObj, TKeys extends (keyof TObj)[]>(obj: TObj, picked: TKeys) => { return picked.reduce((acc, key) => { acc[key] = obj[key]; return acc;
阅读全文
摘要:Problem for partial inference: export const makeSelectors = < TSource, TSelectors extends Record<string, (source: TSource) => any> = {}, >( selectors:
阅读全文
摘要:For the following code: import { CSSProperties } from 'react'; const useStyled = <TTheme = {}>(func: (theme: TTheme) => CSSProperties) => { return {}
阅读全文
摘要:const returnsValueOnly = <T>(t: T) => { return t; } const result = returnsValueOnly("a"); // const returnsValueOnly: <"a">(t: "a") => "a" const return
阅读全文
摘要:For example we want to have one object merge into Window with type information as well. import { Equal, Expect } from "../helpers/type-utils"; const a
阅读全文
摘要:import { Equal, Expect } from '../helpers/type-utils'; export function makeEventHandlers< T extends { [Key in keyof T]: (key: Key) => void } >(obj: T)
阅读全文
摘要:So we have the configObj look like this: export const configObj = { routes: ["/", "/about", "/contact"], fetchers: { // @ts-expect-error "/does-not-ex
阅读全文
摘要:From ts-toolbelt /** * Explain to TS which function parameter has priority for generic inference * @param A to de-prioritize * @returns `A` * @example
阅读全文

浙公网安备 33010602011771号