随笔分类 - TypeScript
摘要:import { getAnimatingState } from "fake-animation-lib"; import { Equal, Expect } from "../helpers/type-utils"; const animatingState = getAnimatingStat
阅读全文
摘要:import React from 'react'; declare global { namespace JSX { interface IntrinsicElements { 'custom-element': { children?: React.ReactNode; title?: stri
阅读全文
摘要:import { Equal, Expect } from '../helpers/type-utils'; import { F } from 'ts-toolbelt'; interface Fruit { name: string; price: number; } export const
阅读全文
摘要:import { it } from 'vitest'; interface Events { click: { x: number; y: number; }; focus: undefined; } type LookUpEvents<K extends keyof Events> = Even
阅读全文
摘要:import { expect, it } from "vitest"; import { fetchUser } from "fake-external-lib"; type Middleware<TInput, TOutput> = (input: TInput) => TOutput; cla
阅读全文
摘要:From previous post, Builder pattern - 03 If we do the following changes: - class TypeSafeStringMap<TMap extends Record<string, string> = {}> { + class
阅读全文
摘要:import { expect, it } from 'vitest'; class TypeSafeStringMap<TMap extends Record<string, string> = {}> { private map: TMap; constructor() { this.map =
阅读全文
摘要:export class BuilderTuple<TList extends any[] = []> { list: TList; constructor() { this.list = [] as any; } push<TNum extends number>(num: TNum): Buil
阅读全文
摘要:The builder pattern is a design pattern commonly used in OOP. It is used to create complex objects step by step throught a series of methods, each of
阅读全文
摘要:file1: import { expect, it } from "vitest"; /** * Here, we've actually got _multiple_ problem files! * Make sure to to check problem.2.ts too. */ decl
阅读全文
摘要:We have the following code: class Form<TValues> { error?: string; constructor( public values: TValues, private validate: (values: TValues) => string |
阅读全文
摘要:import { it } from 'vitest'; import { Brand } from '../helpers/Brand'; type Valid<T> = Brand<T, 'Valid'>; interface PasswordValues { password: string;
阅读全文
摘要:import { it } from 'vitest'; import { Equal, Expect } from '../helpers/type-utils'; export const isDivElement = (element: unknown): element is HTMLDiv
阅读全文
摘要:import { expect, it } from "vitest"; import { Equal, Expect } from "../helpers/type-utils"; export const values = ["a", "b", undefined, "c", undefined
阅读全文
摘要:import { Equal, Expect } from "../helpers/type-utils"; const obj = { a: 1, b: 2, c: 3, } as const; type ObjKey = keyof typeof obj; //Type '"a"' is not
阅读全文
摘要:// You'll need to use function overloads to figure this out! function useData<T>(params: { fetchData: () => Promise<T>; initialData?: T }): { getData:
阅读全文
摘要:Define function / variable in global scope globalThis.myFunc = () => true; // doesn't compile globalThis.myVar = 1; // doesn't compile it("Should let
阅读全文
摘要:In this exercise, we're going to look at a really interesting property of branded types when they're used with index signatures. Here we have our User
阅读全文
摘要:type A = {other: 'string', url: 'string'} type B = {other: 'string', ids: 'string'} type Exclusive< T extends Record<PropertyKey, unknown>, U extends
阅读全文
摘要:import { Equal, Expect } from "../helpers/type-utils"; const obj = { a: 1, b: 2, c: 3, } as const; type ObjKey = keyof typeof obj; // If don't pass in
阅读全文