-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2852-omit-by-type.ts
51 lines (40 loc) · 1.3 KB
/
2852-omit-by-type.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
2852 - OmitByType
-------
by jiangshan (@jiangshanmeta) #medium #object
### Question
From ```T```, pick a set of properties whose type are not assignable to ```U```.
For Example
```typescript
type OmitBoolean = OmitByType<{
name: string
count: number
isReadonly: boolean
isEnable: boolean
}, boolean> // { name: string; count: number }
```
> View on GitHub: https://tsch.js.org/2852
*/
/* _____________ Your Code Here _____________ */
type OmitByType<T extends Record<PropertyKey, any>, U> = {
[key in keyof T as (T[key] extends U ? never : key)]: T[key];
}
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
interface Model {
name: string
count: number
isReadonly: boolean
isEnable: boolean
}
type cases = [
Expect<Equal<OmitByType<Model, boolean>, { name: string; count: number }>>,
Expect<Equal<OmitByType<Model, string>, { count: number; isReadonly: boolean; isEnable: boolean }>>,
Expect<Equal<OmitByType<Model, number>, { name: string; isReadonly: boolean; isEnable: boolean }>>,
]
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/2852/answer
> View solutions: https://tsch.js.org/2852/solutions
> More Challenges: https://tsch.js.org
*/