Skip to content

Commit a39b053

Browse files
author
Andrii Kirmas
committed
Fix any key of inheriting classnames-less component to be never
1 parent 3b70efb commit a39b053

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

src/defs.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ describe("ClassNames", () => {
7070
}
7171
expect(suites).toBeInstanceOf(Object)
7272
})
73+
74+
it("nothing to pick", () => {
75+
type NoClassNames = ClassNames<true>
76+
const suites: Record<string, ClassNames<NoClassNames>> = {
77+
"nothing": {classnames: {}}
78+
}
79+
expect(suites).toBeInstanceOf(Object)
80+
})
7381
})
7482
describe("Miss-use", () => {
7583
it("<'class1', true>", () => {

src/defs.test.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component } from "react";
2-
import type { ClassNamesProp, GetClassKeys } from "./defs";
2+
import type { ClassNamesProp, GetClassKeys, GetClassNames } from "./defs";
33

44
class ClassComponent0 extends Component<ClassNamesProp<"comp1"|"comp2">> {}
55

@@ -13,4 +13,44 @@ describe("GetClassKeys", () => {
1313
}
1414
expect(suites).toBeInstanceOf(Object)
1515
})
16+
17+
it("no classNames", () => {
18+
const suites: Record<string, GetClassKeys<{}>> = {
19+
//@ts-expect-error is not assignable to type 'never'
20+
"undefined": undefined,
21+
//@ts-expect-error is not assignable to type 'never'
22+
"false": false,
23+
//@ts-expect-error is not assignable to type 'never'
24+
"null": null,
25+
//@ts-expect-error is not assignable to type 'never'
26+
"": "",
27+
//@ts-expect-error is not assignable to type 'never'
28+
"0": 0,
29+
//@ts-expect-error is not assignable to type 'never'
30+
"{}": {}
31+
}
32+
expect(suites).toBeInstanceOf(Object)
33+
})
34+
})
35+
36+
describe("GetClassNames", () => {
37+
it("props without classnames", () => {
38+
39+
const suites: Record<string, GetClassNames<{}>> = {
40+
//@ts-expect-error is not assignable to type 'never'
41+
"undefined": undefined,
42+
//@ts-expect-error is not assignable to type 'never'
43+
"false": false,
44+
//@ts-expect-error is not assignable to type 'never'
45+
"null": null,
46+
//@ts-expect-error is not assignable to type 'never'
47+
"": "",
48+
//@ts-expect-error is not assignable to type 'never'
49+
"0": 0,
50+
//@ts-expect-error is not assignable to type 'never'
51+
"{}": {}
52+
}
53+
expect(suites).toBeInstanceOf(Object)
1654
})
55+
})
56+

src/defs.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ export type ClassNamesMap<C extends string> = Record<C, ClassValue>
5555

5656
type GetProps<C> = C extends JSXElementConstructor<infer P> ? P : C
5757
//TODO Consider not empty object
58-
type GetClassNames<T, K = "classnames", D = EmptyObject> = [T] extends [never] ? D : K extends keyof T ? T[K] : never
59-
export type GetClassKeys<C> = keyof GetClassNames<GetProps<C>>
58+
export type GetClassNames<T, D = EmptyObject, K = "classnames"> = [T] extends [never] ? D : K extends keyof T ? T[K] : never
59+
export type GetClassKeys<C> = [GetClassNames<GetProps<C>, never>] extends [never] ? never : keyof GetClassNames<GetProps<C>>
6060

6161
type Ever<T, V> = [T] extends [never] ? EmptyObject : V
6262
export type EmptyObject = Record<never, never>
@@ -66,3 +66,4 @@ export type Falsy = undefined|null|false|0|""
6666
export type ToggleMap<K extends string> = Partial<Record<K, true|Falsy>>
6767

6868
// type get<T, K> = K extends keyof T ? T[K] : never
69+

0 commit comments

Comments
 (0)