Passed
Push — main ( 276544...88f839 )
by Andrii
02:08
created

pipe-with-left.ts ➔ exclusion   A

Complexity

Conditions 3

Size

Total Lines 29
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 24
dl 0
loc 29
rs 9.304
c 0
b 0
f 0
1
import { ClassValue } from "../../defs"
2
3
export {}
4
5
type tExcluder<
6
  Source extends Record<string, ClassValue>,
7
  Left extends Record<string, ClassValue> = Source,
8
  Used extends Record<string, ClassValue> = Record<never, ClassValue>,
9
>
10
= (
11
  <
12
    // E extends 
13
    E extends {[K in keyof Left]?: ClassValue} & {[K in keyof Used]?: never}
14
    // E extends string extends keyof Left
15
    // ? {[K in keyof Used]: never}
16
    // : {[K in keyof Left]?: ClassValue} 
17
  >(exclude: E) =>
18
  //  keyof E extends keyof S ?
19
   { [K in Exclude<keyof Left, keyof E>]: ClassValue; }
20
    & tExcluder<
21
      Source,
22
      { [P in Exclude<keyof Left, keyof E>]: ClassValue; },
23
      { [P in keyof Used | keyof E]: ClassValue; }
24
    >
25
  // : {[P in Exclude<keyof E, keyof S>]: never;}
26
);
27
28
29
function exclusion<
30
  S extends Record<string, ClassValue>,
31
  E extends {[K in keyof S]?: ClassValue}
32
>(
33
  source: S, ex: E
34
) {
35
36
  const filtered = {...source}
37
  for (const k in ex) {
38
    delete filtered[k]
39
  }
40
  
41
  const host = (
42
    e: { [P in Exclude<keyof S, keyof E>]?: ClassValue; }
43
  ) => exclusion(
44
    filtered as { [P in Exclude<keyof S, keyof E>]: ClassValue; },
45
    e
46
  )
47
48
  for (const key in filtered)
49
    //@ts-expect-error
50
    host[key]
51
    = filtered[key]
52
53
  return host as tExcluder<
54
    S,
55
    {[P in Exclude<keyof S, keyof E>]: ClassValue;}
56
  >
57
}
58
59
const source: Record<"a"|"b"|"c"|"d"|"e", ClassValue> = {a: "a", b: undefined, c: "c", d: undefined, e: undefined}
60
61
const step0 = exclusion(
62
  source,
63
  //@ts-expect-error 'z' does not exist in type
64
  {z: undefined}
65
)
66
, answ0: typeof step0 = {
67
  //@ts-expect-error
68
  whatever: true
69
}
70
, step1 = exclusion(source, {a: "a", b: undefined})
71
72
//@ts-expect-error
73
step1({"c": undefined, "z": undefined})
74
()
75
76
const step2 = step1({"c": undefined})
77
//@ts-expect-error
78
, {c} = step2
79
, step3 = {...step2({
80
  //@ts-expect-error
81
  "z": ""
82
})}
83
, result = {...step2}
84
, checks: Record<string, typeof result> = {
85
  "output": {d: "", e: ""},
86
  "unknown": {
87
    //@ts-expect-error
88
    unknown: ""
89
  },
90
  //@ts-expect-error
91
  "lost": {
92
    d: ""
93
  },
94
  "previously ommited": {
95
    d: "", e: "",
96
    //@ts-expect-error
97
    a: ""
98
  }
99
}
100
101
const unknown0 = exclusion({} as Record<string, ClassValue>, {a: "a"})
102
//todo @ts-error
103
, unknown1 = unknown0({a: "a"})
104
105
export {answ0, step3, checks, unknown1}
106