随笔分类 - TypeScript
摘要:type ErrorWithMessage = { message: string } function isErrorWithMessage(error: unknown): error is ErrorWithMessage { return ( typeof error 'object' &&
阅读全文
摘要:async function runWithConcurrency(items, worker, maxConcurrency) { if (!items?.length) return; let i = 0; const workers: Array<Promise<void>> = []; as
阅读全文
摘要:Runner function: function run(genFn) { const it = genFn() let state = { done: false, value: undefined } step() function step(isError, arg) { try { sta
阅读全文
摘要:You’re seeing that const scores = {} satisfies Record<string,number> does check at compile-time “hey, {} can be a Record<string,number>”, but it doesn
阅读全文
摘要:// 内置工具类型 // 1. Partial<T>:将类型 T 的所有属性变为可选属性。 // 2. Required<T>:将类型 T 的所有可选属性变为必需属性。 // 3. Readonly<T>:将类型 T 的所有属性变为只读属性。 // 4. Record<K, T>:将类型 K 的所有
阅读全文
摘要:type Watcher<T> = { on<K extends keyof T & string>( eventName: `${K}Changed`, callback: (oldValue: T[K], newValue: T[K]) => void ): void; }; declare f
阅读全文
摘要:Using T extends any ? {...} : never helps to see the type result Example: type DeepReadonly<T extends Record<string, any>> = T extends any ? { readonl
阅读全文
摘要:export function singleton< T extends new (...args: any[]) => object >(classCtor: T): T { let instance!: InstanceType<T> const proxy = new Proxy(classC
阅读全文
摘要:The example case covers room numbers validation: it must start with an uppercase letter followed by three digits. z.custom allows the creation of a br
阅读全文
摘要:Instead of creating many small schemas, which can complicate maintenance, Zod allows for the extraction of specific parts of a schema using the concep
阅读全文
摘要:When you try to import image in typescript project, it will report issue import logo from "./assets/react.svg" What you can do is, create a types.d.ts
阅读全文
摘要:整合 TypeScript 准备工作 首先我们需要有一个基于 ts 的项目。 第一步通过 npm init -y 初始化项目 接下来通过: npm install typescript 局部安装 typescript。 之后还需要生成 typescript 的配置文件,通过命令: npx tsc -
阅读全文
摘要:The Input is Different than the Output We've reached the point with Zod where our input is different than our output. In other words, you can generate
阅读全文
摘要:Define a component with props and defualt props value <script setup lang="ts"> import { ref, onMounted } from 'vue' import fetchCount from '../service
阅读全文
摘要:<template> <router-link class="event-link" :to="{ name: 'EventDetails', params: { id: event.id } }" > <div class="event-card"> <span>@{{ event.time }}
阅读全文
摘要:/** * Utility for extracting the parameters from a function overload (for typed emits) * https://github.com/microsoft/TypeScript/issues/32164#issuecom
阅读全文
摘要:We have a module: const key = Symbol('key') export class A { [key] = 1 value () { console.log(this[key]) } } It seems that keyis not expose to outside
阅读全文
摘要:const BookingSchema = z.object({ roomType: z.string(), dueDate: z .string({ message: "invalid due date", }) .date('date format is wrong'), numberOfGue
阅读全文
摘要:export enum EffectFlags { /** * ReactiveEffect only */ ACTIVE = 1 << 0, RUNNING = 1 << 1, TRACKING = 1 << 2, NOTIFIED = 1 << 3, DIRTY = 1 << 4, ALLOW_
阅读全文
摘要:type BanType<T, E> = T extends E ? never : T; type BanDate<T> = BanType<T, Date>; function log<T>(x: BanDate<T>) { console.log() } log(new Date()) //
阅读全文