Passed
Push — main ( c4701f...95102b )
by Andrii
02:26
created

src/sandbox/omitters/exclude-extends-source.ts   A

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 50
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 2
mnd 1
bc 1
fnc 1
bpm 1
cpm 2
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A exclude-extends-source.ts ➔ exclusion 0 25 2
1
import { ClassValue } from "../../defs"
2
3
export {}
4
5
// type Excluder<
6
//   S extends Record<string, ClassValue>,
7
//   E extends {[K in keyof S]?: ClassValue}
8
// > = { [P in Exclude<keyof S, keyof E>]: S[P]; }
9
10
// interface Exclusion<
11
//   S extends Record<string, ClassValue>
12
// > {
13
//   (source: S, ex: {[K in keyof S]?: ClassValue}): Excluder<S, typeof ex>
14
// }
15
16
function exclusion<
17
  S extends Record<string, ClassValue>,
18
  E extends {[K in keyof S]?: ClassValue}
19
>(
20
  source: S, ex: E
21
): { [P in Exclude<keyof S, keyof E>]: S[P]; }
22
{
23
  const $return = {...source}
24
  for (const k in ex) {
25
    delete $return[k]
26
  }
27
28
  return $return
29
}
30
31
const source: Record<"a"|"b"|"c"|"d"|"e", ClassValue> = {a: "a", b: undefined, c: "c", d: undefined, e: undefined}
32
33
const step0 = exclusion(
34
  source,
35
  //@ts-expect-error 'z' does not exist in type
36
  {z: undefined}
37
)
38
, answ0: typeof step0 = {
39
  whatever: true
40
}
41
, step1 = exclusion(source, {a: "a", b: undefined})
42
, step2 = exclusion(step1, {"c": undefined})
43
//@ts-expect-error Property 'd' is missing
44
, answ
45
: typeof step2 = {
46
  e: "",
47
  //@ts-expect-error Object literal may only specify known properties, and 'z'
48
  z: ""
49
}
50
export {step2, answ0}