Passed
Push — main ( 0c937b...04c127 )
by Andrii
02:29
created

ts-swiss.types.test.ts ➔ keying   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
1
import type { Subest } from "./ts-swiss.types"
2
3
describe("Subest", () => {
4
5
  type SubFunc<Base> = <T extends {[K in keyof Base]?: Base[K]}>(arg: Subest<T, Base>) => (keyof T)[]
6
7
  function keying<Base>() {
8
    return (arg => Object.keys(arg)) as SubFunc<Base>
9
  }
10
11
  type FlatOpt = {
12
    opt1?: "a"|"b"|"c"
13
    opt2?: string
14
  }
15
16
  type FlatReq = {
17
    req1: "a"|"b"|"c"
18
    req2: string
19
  }
20
21
  it("demo", () => {
22
    const retOpt = keying<FlatOpt>()({opt1: "a"})
23
    , retReq = keying<FlatReq>()({req1: "a"})
24
    , checkOpt: Record<string, typeof retOpt> = {
25
      "exact": ["opt1"],
26
      "redundant": ["opt1", 
27
        //@ts-expect-error
28
        "opt2"
29
      ]
30
    }
31
    , checkReq: Record<string, typeof retReq> = {
32
      "exact": ["req1"],
33
      "redundant": ["req1", 
34
        //@ts-expect-error
35
        "req2"
36
      ]
37
    }
38
    
39
    expect({checkOpt, checkReq}).toBeInstanceOf(Object)
40
  })
41
42
  it("flat", () => {  
43
    const check1: Record<string, Subest<{opt1: "a"|"b"}, FlatOpt>> = {
44
      "exact": {opt1: "a"},
45
      //@ts-expect-error
46
      "wrong value": {opt1: "X"},
47
      "redundant property": {opt1: "a",
48
        //@ts-expect-error
49
        redundant: "x"
50
      }
51
    }
52
53
    expect({check1}).toBeInstanceOf(Object)
54
  })
55
})