Skip to content

Commit 04c1276

Browse files
author
Andrii Kirmas
committed
#32 Add Subest test
1 parent 0c937b4 commit 04c1276

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

src/ts-swiss.types.test.ts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
})

src/ts-swiss.types.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ export type Cut<Str extends string, Delimiter extends string, toNever extends bo
2828
> = Str extends `${string}${Delimiter}${infer Back}` ? Back : toNever extends false ? Str : never
2929
export type NoSubString<Str extends string, Sub extends string> = Exclude<Str, `${string}${Sub}${string}`>
3030

31-
export type Subest<Base, Extendent> = string extends keyof Base
32-
? Extendent
33-
: Base extends Extendent
34-
? Extendent
35-
: Base
31+
export type Subest<Extended, Base> = string extends keyof Base
32+
? Extended
33+
: Base extends Extended
34+
? Extended
35+
: {[K in keyof Base]?: Base[K]}
3636

3737
export type Extends<T, V, X> = [T extends V ? true : never] extends [never] ? never : X
3838

0 commit comments

Comments
 (0)