Passed
Push — main ( b7e3d1...276544 )
by Andrii
01:56
created

mix.ts ➔ classNaming   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
export {}
2
type ClassHash = string | undefined
3
4
type Props = {
5
  Btn: ClassHash
6
  isValid: boolean
7
  isOpen?: boolean
8
}
9
10
const {
11
  Btn,
12
  isOpen,
13
  isValid
14
} = {} as Props
15
16
classNaming(
17
  guard1({Btn, Btn__Open: isOpen, Btn__Valid: isValid})
18
)
19
//@ts-expect-error
20
guard2({Btn, Btn__Open: isOpen, Btn__Valid: isValid})
21
22
function guard1<
23
  E extends Record<string, ClassHash|boolean> 
24
>(source: E) { 
25
  return source as unknown as {[K in keyof E]: E[K] extends boolean ? boolean : E[K] extends ClassHash ? ClassHash : never}
26
}
27
28
function guard2<T>(source: T extends Record<infer K, ClassHash|boolean> ? {
29
  [k in K]: T[k] extends boolean ? T[k] : T[k] extends ClassHash ? T[k] : never
30
 } : never) { 
31
  return source 
32
}
33
34
function classNaming<T>(source: T) {
35
  return source
36
}