随笔分类 - TypeScript
摘要:Implement type IsPalindrome<T> to check whether a string or number is palindrome. For example: IsPalindrome<'abc'> // false IsPalindrome<121> // true
阅读全文
摘要:Implement the type version of Object.fromEntries For example: interface Model { name: string; age: number; locations: string[] | null; } type ModelEnt
阅读全文
摘要:Implement a generic GetReadonlyKeys<T> that returns a union of the readonly keys of an Object. For example interface Todo { readonly title: string rea
阅读全文
摘要:Implement a generic IsRequiredKey<T, K> that return whether K are required keys of T . For example type A = IsRequiredKey<{ a: number, b?: string },'a
阅读全文
摘要:Implement the generic ClassPublicKeys<T> which returns all public keys of a class. For example: class A { public str: string protected num: number pri
阅读全文
摘要:The well known split() method splits a string into an array of substrings by looking for a separator, and returns the new array. The goal of this chal
阅读全文
摘要:Drop the specified chars from a string. For example: type Butterfly = DropString<'foobar!', 'fb'> // 'ooar!' /* _____________ Your Code Here _________
阅读全文
摘要:Implement Camelize which converts object from snake_case to to camelCase Camelize<{ some_prop: string, prop: { another_prop: string }, array: [{ snake
阅读全文
摘要:import { assign, createMachine } from "xstate"; type FetchStates = | { value: "idle"; context: FetchContext & { results: []; message: ""; }; } | { val
阅读全文
摘要:Create a type-level function whose types is similar to Pinia library. You don't need to implement function actually, just adding types. Overview This
阅读全文
摘要:import React, { useState } from "react"; type Base = { id: string } | string; type GenericSelectProps<TValue> = { formatLabel: (value: TValue) => stri
阅读全文
摘要:Implement a type DeepPick, that extends Utility types Pick. A type takes two arguments. For example: type obj = { name: 'hoge', age: 20, friend: { nam
阅读全文
摘要:const obj = { name: "John", age: 33, cars: [ { make: "Ford", age: 10 }, { make: "Tesla", age: 2 }, ], } as const; export type PathKeys<T> = T extends
阅读全文
摘要:MethodDecorator @Log(level): Console log message @Pref: Mesure time function Log(level: LoggingLevel): MethodDecorator { return ( target: any, propert
阅读全文
摘要:Create a type-safe string join utility which can be used like so: const hyphenJoiner = join('-') const result = hyphenJoiner('a', 'b', 'c'); // = 'a-b
阅读全文
摘要:Implement a type, UnionToTuple, that converts a union to a tuple. As we know, union is an unordered structure, but tuple is an ordered, which implies
阅读全文
摘要:Let's say you extends from a base class, you intent to override a method in base class class BaseCmp { showCmp() {} hideCmp() {} helperMethod() {} } c
阅读全文
摘要:You can give Getter or Setter different types. So that Setter can accpet a wider range of types, while Getter can return a narrow type. class Thing {
阅读全文
摘要:Implement a type LengthOfString<S> that calculates the length of the template string (as in 298 - Length of String): type T0 = LengthOfString<"foo"> /
阅读全文
摘要:Typescript has NonNullable<T>, let's build a Nullable<T> type Nullable<T extends Record<PropertyKey, unknown>> = { [K in keyof T]: T[K] | null } type
阅读全文