随笔分类 -  TypeScript

上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 30 下一页
摘要: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 阅读全文
posted @ 2022-12-21 22:41 Zhentiw 阅读(24) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-12-19 15:32 Zhentiw 阅读(20) 评论(0) 推荐(0)
摘要: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  阅读全文
posted @ 2022-12-16 15:28 Zhentiw 阅读(22) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-12-15 20:39 Zhentiw 阅读(632) 评论(0) 推荐(0)
摘要: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< 阅读全文
posted @ 2022-12-15 16:03 Zhentiw 阅读(21) 评论(0) 推荐(0)
摘要:import { Equal, Expect } from "../helpers/type-utils"; type Route = | { route: "/"; search: { page: string; perPage: string; }; } | { route: "/about" 阅读全文
posted @ 2022-12-14 16:07 Zhentiw 阅读(27) 评论(0) 推荐(0)
摘要:import { Equal, Expect } from "../helpers/type-utils"; interface Attributes { id: string; email: string; username: string; } /** * How do we create a 阅读全文
posted @ 2022-12-14 15:33 Zhentiw 阅读(21) 评论(0) 推荐(0)
摘要:Let's say we want to extract query param from string: type UserPath = "/users/:id"; type UserOrganisationPath = "/users/:id/organisations/:organisatio 阅读全文
posted @ 2022-12-14 15:24 Zhentiw 阅读(30) 评论(0) 推荐(0)
摘要:Consider this discriminated union called Fruit: type Fruit = | { name: "apple"; color: "red"; } | { name: "banana"; color: "yellow"; } | { name: "oran 阅读全文
posted @ 2022-12-13 15:42 Zhentiw 阅读(36) 评论(0) 推荐(0)
摘要:We start with a Values interface: interface Values { email: string; firstName: string; lastName: string; } We want a union of tuple [key, value]as res 阅读全文
posted @ 2022-12-13 15:31 Zhentiw 阅读(31) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2022-12-13 15:26 Zhentiw 阅读(30) 评论(0) 推荐(0)
摘要:Let's say we have: type Fruit = "apple" | "banana" | "orange"; We only want AppleOrBanana If we do as such: type Fruit = "apple" | "banana" | "orange" 阅读全文
posted @ 2022-12-12 15:52 Zhentiw 阅读(22) 评论(0) 推荐(0)
摘要:Let's imagine you're building a type helper to extract out the value from several different 'parsers'. const parser1 = { parse: () => 1, }; const pars 阅读全文
posted @ 2022-12-12 15:40 Zhentiw 阅读(18) 评论(0) 推荐(0)
摘要:Example 1 import { S } from "ts-toolbelt"; import { Equal, Expect } from "../helpers/type-utils"; type Names = [ "Matt Pocock", "Jimi Hendrix", "Eric 阅读全文
posted @ 2022-12-12 15:20 Zhentiw 阅读(95) 评论(0) 推荐(0)
摘要:In this exercise we have an interface MyComplexInterface which is acting as a type helper. The interface takes arguments for Event, Context, Name, and 阅读全文
posted @ 2022-12-12 15:08 Zhentiw 阅读(22) 评论(0) 推荐(0)
摘要:import { Equal, Expect } from "../helpers/type-utils"; type GetPropValue< T extends Record<PropertyKey, any>, P extends keyof T = "data" > = T extends 阅读全文
posted @ 2022-12-11 22:33 Zhentiw 阅读(36) 评论(0) 推荐(0)
摘要:type NonEmptyArray<T> = [T, ...Array<T>]; export const makeEnum = (values: NonEmptyArray<string>) => {}; makeEnum(["a"]); makeEnum(["a", "b", "c"]); / 阅读全文
posted @ 2022-12-11 22:16 Zhentiw 阅读(15) 评论(0) 推荐(0)
摘要:export type Maybe<T extends {}> = T | null | undefined; type tests = [ // @ts-expect-error Maybe<null>, // @ts-expect-error Maybe<undefined>, Maybe<st 阅读全文
posted @ 2022-12-11 22:11 Zhentiw 阅读(21) 评论(0) 推荐(0)
摘要:import { Equal, Expect } from "../helpers/type-utils"; type Maybe<T> = T | null | undefined; type tests = [ Expect<Equal<Maybe<string>, string | null 阅读全文
posted @ 2022-12-11 22:03 Zhentiw 阅读(15) 评论(0) 推荐(0)
摘要:import { Equal, Expect } from "../helpers/type-utils"; type Identity<T> = T; type tests = [ Expect<Equal<Identity<1>, 1>>, Expect<Equal<Identity<"1">, 阅读全文
posted @ 2022-12-11 22:01 Zhentiw 阅读(32) 评论(0) 推荐(0)

上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 30 下一页