Skip to content

Commit 1fe375b

Browse files
author
Andrii Kirmas
committed
#9 Add to ClassNamesProperty module-based support. Catch #12
1 parent 95588ac commit 1fe375b

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

src/defs.test.ts

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,75 @@ import {
22
Component,
33
// ReactElement
44
} from "react"
5-
import type { ClassNamed, ClassHash, ReactRelated, GetProps} from "./defs"
5+
import type {
6+
ClassNamed,
7+
ClassHash,
8+
ReactRelated,
9+
GetProps,
10+
ClassNamesProperty
11+
} from "./defs"
612

7-
export {}
13+
describe("ClassNamesProperty", () => {
14+
it("Free declaration", () => {
15+
type Props = ClassNamesProperty<{
16+
class1: ClassHash, class2: ClassHash
17+
}>
18+
const suites: Record<string, Props["classnames"]> = {
19+
"all setted": {
20+
class1: "class1",
21+
class2: undefined
22+
},
23+
"redundant": {
24+
class1: "class1",
25+
class2: undefined,
26+
//@ts-expect-error
27+
redundant: "redundant"
28+
},
29+
//@ts-expect-error
30+
"missed": {
31+
class1: "class1"
32+
},
33+
"wrong type": {
34+
class1: "class1",
35+
//@ts-expect-error
36+
class2: false
37+
}
38+
}
39+
expect(suites).toBeInstanceOf(Object)
40+
})
41+
it("Module based", () => {
42+
type CssModule = {
43+
App: ClassHash
44+
class1: ClassHash, class2: ClassHash
45+
}
846

9-
type Getter<T extends ReactRelated> = GetProps<T>
47+
type Props = ClassNamesProperty<CssModule, {
48+
class1: ClassHash
49+
class2: ClassHash
50+
//TODO #12 Why no suggestion?
51+
}>
52+
53+
type PropsWithWrong = ClassNamesProperty<CssModule,
54+
//@ts-expect-error
55+
{class3: ClassHash}
56+
>
57+
58+
const suite4wrong: PropsWithWrong["classnames"] = {
59+
class3: undefined,
60+
},
61+
suite: Props["classnames"] = {
62+
class1: "class1",
63+
class2: undefined
64+
}
65+
expect({suite4wrong, suite}).toBeInstanceOf(Object)
66+
})
67+
})
1068

1169
it.todo("ClassNamesFrom")
1270

1371
describe("ReactRelated", () => {
72+
type Getter<T extends ReactRelated> = GetProps<T>
73+
1474
type Without = ClassNamed
1575
type Wrong = ClassNamed & {classnames: string}
1676
type Some = ClassNamed & {classnames: Record<string, ClassHash>}
@@ -64,4 +124,4 @@ describe("ReactRelated", () => {
64124
})
65125

66126

67-
})
127+
})

src/defs.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export type ClassNames<
2525
C10 extends ReactRelated = never
2626
>
2727
= Ever<Extract<C0, true>, ClassNamed>
28-
& ClassNamesProperty<
28+
& ClassNamesCombiner<
2929
& Ever<Extract<C0, ReactRelated>, ClassNamesFrom<Extract<C0, ReactRelated>>>
3030
& Ever<C1, ClassNamesFrom<C1>>
3131
& Ever<C2, ClassNamesFrom<C2>>
@@ -39,8 +39,10 @@ export type ClassNames<
3939
& Ever<C10, ClassNamesFrom<C10>>
4040
>
4141

42-
//TODO #9 Add leading `map` to check
43-
export type ClassNamesProperty<C extends CssModule> = Ever<C, Ever<keyof C, {classnames: {[K in keyof C]: ClassHash}}>>
42+
export type ClassNamesProperty<
43+
C extends CssModule, T extends {[K in keyof C]?: ClassHash} = C
44+
> = {classnames: {[K in keyof T & keyof C]: ClassHash}}
45+
type ClassNamesCombiner<C extends CssModule> = Ever<C, Ever<keyof C, {classnames: {[K in keyof C]: ClassHash}}>>
4446

4547
export type ClassHash = undefined|string
4648

@@ -56,8 +58,7 @@ export type ClassNamingContext<T extends CssModule> = Partial<ClassNamed> & {
5658
}
5759

5860
/// iNTERNAL
59-
60-
type WithClassNames = ClassNamesProperty<CssModule>
61+
type WithClassNames = {classnames: CssModule}
6162

6263
export type CssModule = Record<string, ClassHash>
6364
//TODO Consider not empty object

0 commit comments

Comments
 (0)