[Typescript] 70. Medium - Without
typescript Medium Without 70
2023-09-14 08:59:11 时间
Implement the type version of Lodash.without, Without<T, U> takes an Array T, number or array U and returns an Array without the elements of U.
type Res = Without<[1, 2], 1>; // expected to be [2]
type Res1 = Without<[1, 2, 4, 1, 5], [1, 2]>; // expected to be [4, 5]
type Res2 = Without<[2, 3, 2, 3, 2, 3, 2, 3], [2, 3]>; // expected to be []
/* _____________ Your Code Here _____________ */
type Without<T extends any[], U extends any[] | any> = T extends [infer F, ...infer RT]
? U extends any[] // U is array like
? F extends U[number] // F in U array
? Without<RT, U> // exlcude F
: [F, ...Without<RT, U>] // include F
: F extends U // U is not an array like
? Without<RT, U> // exlcude F
: [F, ...Without<RT, U>] // include F
: [];
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Without<[1, 2], 1>, [2]>>,
Expect<Equal<Without<[1, 2, 4, 1, 5], [1, 2]>, [4, 5]>>,
Expect<Equal<Without<[2, 3, 2, 3, 2, 3, 2, 3], [2, 3]>, []>>,
]
相关文章
- [Typescript 4.9] Satisfies operator
- [Typescript] 91. Hard - Union to Intersection
- [Typescript] 62. Medium - Fibonacci Sequence
- [Typescript] 58. Medium - BEM style string
- [Typescript] 46. Medium - PickByType
- [Typescript] 27. Medium - Capitalize
- [React Typescript 2022] Type a Radio Button Component in React using TypeScript
- [Typescript] Ignore Null or Undefined Values with TypeScript Non-Null Assertion Operator
- [Typescript] Performance Bundling and tslib
- [TypeScript] Define Custom Type Guard Functions in TypeScript
- [Typescript] Create a Type-Safe Request Handler with Zod and Express
- [Typescript] 136. Medium - Object to Union
- [Typescript] 131. Extreme - Query String Parser
- [Typescript] 85. Medium - IsInteger
- [Typescript] 67. Medium - Chunk
- [Typescript] 51. Medium - Mutable
- [Typescript] 44. Medium - Drop char
- [Typescript challenge] 19. Medium - Tuple to Union
- [Typescript] Making TypeScript Stick - 5 - Extends
- [React Typescript 2022] Use TypeScript to Type a React Class Component
- [Typescript] Typing “this” and “noImplicitThis”
- [Typescript] Exclude Properties from a Type in TypeScript (Omit)
- [Typescript] Improve Readability with TypeScript Numeric Separators when working with Large Numbers
- [TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types
- [TypeScript] Increase TypeScript's type safety with noImplicitAny
- 如何找到 SAP Spartacus 里某个 Component TypeScript 编译后生成的 JavaScript 位置