12 2022 档案
摘要:Array.from() is a great way to chunk up arrays because of the secondary argument being a map function. const hugeArray = Array.from({length: 76}, (_,
阅读全文
摘要:The Intl.Collator object enables language-sensitive string comparison. console.log(['Z', 'a', 'z', 'ä'].sort(new Intl.Collator('de').compare)); // exp
阅读全文
摘要:Since arrays are objects, we can destructure their indexes to easily grab the first and last itmes const bikes = ['bianchi', 'miele', 'miyata', 'benot
阅读全文
摘要:For example, we have a type repersent currency: USD, value should be something like '$123', a string type with `$` and number. So let's say we make a
阅读全文
摘要:Currying is the technique of converting a function that takes multiple arguments into a sequence of functions that each take a single argument. But in
阅读全文
摘要:jest.mock('./filename', () => { const originalModule = jest.requireActual('./filename') return { ...originalModule, fnA: jest.fn(), fnB: (b: boolean):
阅读全文
摘要:This challenge continues from 476 - Sum, it is recommended that you finish that one first, and modify your code based on it to start this challenge. I
阅读全文
摘要:Implement a type `Sum<A, B>` that summing two non-negative integers and returns the sum as a string. Numbers can be specified as a string, number, or
阅读全文
摘要:// Doesn't work import React from 'react' import { saveInfo } from './api' export default function App() { const [count, setCount] = React.useState(0)
阅读全文
摘要:https://bestofjs.org/projects/valtio Cool things about Valtio, it is completely independ from React component. It is self testable and hook with React
阅读全文
摘要:Implement a type-level integers comparator. We've provided an enum for indicating the comparison result, like this: If a is greater than b, type shoul
阅读全文
摘要:Implement the JavaScript Array.slice function in the type system. Slice<Arr, Start, End> takes the three argument. The output should be a subarray of
阅读全文
摘要:It is useful to enable '@typescript-eslint/unbound-method': 'error', because this kind of error is related to this keyword, sometime it is hard to not
阅读全文
摘要:Create a SnakeCase<T> generic that turns a string formatted in camelCase into a string formatted in snake_case. A few examples: type res1 = SnakeCase<
阅读全文
摘要:import { Equal, Expect } from "../helpers/type-utils"; type Route = | { route: "/"; search: { page: string; perPage: string; }; } | { route: "/about"
阅读全文
摘要:import { Equal, Expect } from "../helpers/type-utils"; interface Attributes { id: string; email: string; username: string; } /** * How do we create a
阅读全文
摘要:Let's say we want to extract query param from string: type UserPath = "/users/:id"; type UserOrganisationPath = "/users/:id/organisations/:organisatio
阅读全文
摘要:Consider this discriminated union called Fruit: type Fruit = | { name: "apple"; color: "red"; } | { name: "banana"; color: "yellow"; } | { name: "oran
阅读全文
摘要:We start with a Values interface: interface Values { email: string; firstName: string; lastName: string; } We want a union of tuple [key, value]as res
阅读全文
摘要:We have a type Route that is a discriminated union of the possible routes in the application. Each route has the properties search and route type Rout
阅读全文
摘要:Let's say we have: type Fruit = "apple" | "banana" | "orange"; We only want AppleOrBanana If we do as such: type Fruit = "apple" | "banana" | "orange"
阅读全文
摘要:Let's imagine you're building a type helper to extract out the value from several different 'parsers'. const parser1 = { parse: () => 1, }; const pars
阅读全文
摘要:Example 1 import { S } from "ts-toolbelt"; import { Equal, Expect } from "../helpers/type-utils"; type Names = [ "Matt Pocock", "Jimi Hendrix", "Eric
阅读全文
摘要:In this exercise we have an interface MyComplexInterface which is acting as a type helper. The interface takes arguments for Event, Context, Name, and
阅读全文
摘要:import { Equal, Expect } from "../helpers/type-utils"; type GetPropValue< T extends Record<PropertyKey, any>, P extends keyof T = "data" > = T extends
阅读全文
摘要:type NonEmptyArray<T> = [T, ...Array<T>]; export const makeEnum = (values: NonEmptyArray<string>) => {}; makeEnum(["a"]); makeEnum(["a", "b", "c"]); /
阅读全文
摘要:export type Maybe<T extends {}> = T | null | undefined; type tests = [ // @ts-expect-error Maybe<null>, // @ts-expect-error Maybe<undefined>, Maybe<st
阅读全文
摘要:import { Equal, Expect } from "../helpers/type-utils"; type Maybe<T> = T | null | undefined; type tests = [ Expect<Equal<Maybe<string>, string | null
阅读全文
摘要:import { Equal, Expect } from "../helpers/type-utils"; type Identity<T> = T; type tests = [ Expect<Equal<Identity<1>, 1>>, Expect<Equal<Identity<"1">,
阅读全文
摘要:Let's say we have type TemplateLiteralKey = `${"user" | "post" | "comment"}${"Id" | "Name"}`; We want to make a type type ObjectOfKeys = unknown; In o
阅读全文
摘要:Here we have a Sandwich type that's currently assigned to unknown We also have a couple of union types, BreadType and Filling, that have several optio
阅读全文
摘要:Let's say we have an object as const: const frontendToBackendEnumMap = { singleModule: "SINGLE_MODULE", multiModule: "MULTI_MODULE", sharedModule: "SH
阅读全文
摘要:Let's say we have a const of object: export const programModeEnumMap = { GROUP: "group", ANNOUNCEMENT: "announcement", ONE_ON_ONE: "1on1", SELF_DIRECT
阅读全文
摘要:For example we have a discriminated union type: export type Event = | { type: "click"; event: MouseEvent; } | { type: "focus"; event: FocusEvent; } |
阅读全文
摘要:Give a discriminated union: export type Event = | { type: "click"; event: MouseEvent; } | { type: "focus"; event: FocusEvent; } | { type: "keydown"; e
阅读全文
摘要:Implement the type Filter<T, Predicate> takes an Array T, primitive type or union primitive type Predicate and returns an Array include the elements o
阅读全文
摘要:Returns true if all elements of the list are equal to the second parameter passed in, false if there are any mismatches. For example type Test1 = [1,
阅读全文
摘要:Imagine you make a super fancy input component as part of your design system. Imagine that a parent element (i.e. the component that renders the fancy
阅读全文
摘要:A new hook for version 18 of React is useId. Frequently in React you need unique identifiers to associate two objects together. An example of this wou
阅读全文
摘要:useLayoutEffect is almost the same as useEffect except that it's synchronous to render as opposed to scheduled like useEffect is. If you're migrating
阅读全文
摘要:You're required to implement a type-level parser to parse URL query string into a object literal type. Some detailed requirements: Value of a key in q
阅读全文
摘要:function validIPAddresses(string) { const res= [] for (let i = 1; i < Math.min(string.length, 4); i++) { const parts = ['', '', '', ''] parts[0] = str
阅读全文
摘要:Given an union of types and array of type pairs to replace ([[string, number], [Date, null]]), return a new union replaced with the type pairs. /* ___
阅读全文
摘要:Capitalize the key of the object, and if the value is an array, iterate through the objects in the array. /* _____________ Your Code Here ____________
阅读全文
摘要:You have a target object and a source array of objects. You need to copy property from source to target, if it has the same property as the source, yo
阅读全文
摘要:Given an array of integers nums and an integer target, return true if two numbers such that they add up to target. /* _____________ Your Code Here ___
阅读全文
摘要:The optimizing compiler optimizes for what it’s seen. If it sees something new, that’s problematic. Seleting properties has some strange implications
阅读全文
摘要:Code: benchmark.js const { performance } = require('perf_hooks'); // SETUP 🏁 let iterations = 1e7; const a = 1; const b = 2; const add = (x, y) => x
阅读全文
摘要:Get all possible paths that could be called by _.get (a lodash function) to get the value of an object type T1 = ObjectKeyPaths<{ name: string; age: n
阅读全文
摘要:Implement BinaryToDecimal<S> which takes an exact string type S consisting 0 and 1 and returns an exact number type corresponding with S when S is reg
阅读全文
摘要:Implement the type version of Lodash.intersection with a little difference. Intersection takes an Array T containing several arrays or any type elemen
阅读全文