随笔分类 -  TypeScript

上一页 1 ··· 3 4 5 6 7 8 9 10 11 ··· 30 下一页
摘要:import {Expect, Equal} from "../types/utils" import { expect, it } from 'vitest'; type PayloadsToDiscriminatedUnion<T extends Record<string, any>> = { 阅读全文
posted @ 2023-05-07 16:00 Zhentiw 阅读(30) 评论(0) 推荐(0)
摘要:interface EnemyAttacker { fireWeapon(): void; driveForward(): void; assignDriver(driverName: string): void; } // Target class EnemyTank implements Ene 阅读全文
posted @ 2023-05-05 01:59 Zhentiw 阅读(25) 评论(0) 推荐(0)
摘要:Example code: interface Fruit { name: string; price: number; } export const wrapFruit = <TFruits extends Fruit[]>(fruits: TFruits) => { const getFruit 阅读全文
posted @ 2023-05-03 01:55 Zhentiw 阅读(59) 评论(0) 推荐(0)
摘要:Original code: import { CSSProperties } from "react"; const useStyled = <TTheme = {}>(func: (theme: TTheme) => CSSProperties) => { // Imagine that thi 阅读全文
posted @ 2023-04-17 14:45 Zhentiw 阅读(27) 评论(0) 推荐(0)
摘要:This the following code example, test have passed for both run time and compile time: import { expect, it } from 'vitest'; import { Equal, Expect } fr 阅读全文
posted @ 2023-04-11 21:09 Zhentiw 阅读(24) 评论(0) 推荐(0)
摘要:export const inferItemLiteral = <T>(t: T) => { return { output: t, }; }; const result1 = inferItemLiteral("a"); // ? {output: string} /* vs */ export 阅读全文
posted @ 2023-04-10 21:31 Zhentiw 阅读(21) 评论(0) 推荐(0)
摘要:There are two options doing the same things. const makeSafe = <TParams extends any[], TReturn>(func: (...args: TParams) => TReturn) => ( ...args: TPar 阅读全文
posted @ 2023-04-10 21:16 Zhentiw 阅读(26) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2023-04-04 01:49 Zhentiw 阅读(35) 评论(0) 推荐(0)
摘要:const demoFunc = <TObj extends {}>(obj: TObj, key: ErrorIfNever<keyof TObj, `You much pass at least one key`>) => { return obj[key as keyof TObj] } ty 阅读全文
posted @ 2023-03-30 00:29 Zhentiw 阅读(23) 评论(0) 推荐(0)
摘要:type TMethodListener<T> = (copyFn: T, ...args: any[]) => void; type TBuildInfo<TOverriden> = { mutationList: TOverriden; error?: string; }; /** * @des 阅读全文
posted @ 2023-03-11 22:50 Zhentiw 阅读(30) 评论(0) 推荐(0)
摘要:import express, { RequestHandler } from 'express'; import { it } from 'vitest'; import { z, ZodError } from 'zod'; import { Equal, Expect } from '../h 阅读全文
posted @ 2023-03-02 16:42 Zhentiw 阅读(76) 评论(0) 推荐(0)
摘要:import { expect, it } from 'vitest'; import { Equal, Expect } from '../helpers/type-utils'; export function compose<T1, T2>(func: (t1: T1) => T2): (t1 阅读全文
posted @ 2023-03-02 03:23 Zhentiw 阅读(33) 评论(0) 推荐(0)
摘要:const pick = <TObj, TKeys extends (keyof TObj)[]>(obj: TObj, picked: TKeys) => { return picked.reduce((acc, key) => { acc[key] = obj[key]; return acc; 阅读全文
posted @ 2023-03-01 22:30 Zhentiw 阅读(43) 评论(0) 推荐(0)
摘要:Problem for partial inference: export const makeSelectors = < TSource, TSelectors extends Record<string, (source: TSource) => any> = {}, >( selectors: 阅读全文
posted @ 2023-02-27 14:58 Zhentiw 阅读(37) 评论(0) 推荐(0)
摘要:For the following code: import { CSSProperties } from 'react'; const useStyled = <TTheme = {}>(func: (theme: TTheme) => CSSProperties) => { return {} 阅读全文
posted @ 2023-02-25 20:45 Zhentiw 阅读(20) 评论(0) 推荐(0)
摘要:const returnsValueOnly = <T>(t: T) => { return t; } const result = returnsValueOnly("a"); // const returnsValueOnly: <"a">(t: "a") => "a" const return 阅读全文
posted @ 2023-02-25 15:53 Zhentiw 阅读(17) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2023-02-24 15:22 Zhentiw 阅读(58) 评论(0) 推荐(0)
摘要:import { Equal, Expect } from '../helpers/type-utils'; export function makeEventHandlers< T extends { [Key in keyof T]: (key: Key) => void } >(obj: T) 阅读全文
posted @ 2023-02-24 15:02 Zhentiw 阅读(27) 评论(0) 推荐(0)
摘要:So we have the configObj look like this: export const configObj = { routes: ["/", "/about", "/contact"], fetchers: { // @ts-expect-error "/does-not-ex 阅读全文
posted @ 2023-02-24 14:58 Zhentiw 阅读(28) 评论(0) 推荐(0)
摘要:From ts-toolbelt /** * Explain to TS which function parameter has priority for generic inference * @param A to de-prioritize * @returns `A` * @example 阅读全文
posted @ 2023-02-23 16:01 Zhentiw 阅读(106) 评论(0) 推荐(0)

上一页 1 ··· 3 4 5 6 7 8 9 10 11 ··· 30 下一页