Skip to content

Commit

Permalink
#9 Add to ClassNamesProperty module-based support. Catch #12
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Kirmas committed Feb 27, 2021
1 parent 95588ac commit 1fe375b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
68 changes: 64 additions & 4 deletions src/defs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,75 @@ import {
Component,
// ReactElement
} from "react"
import type { ClassNamed, ClassHash, ReactRelated, GetProps} from "./defs"
import type {
ClassNamed,
ClassHash,
ReactRelated,
GetProps,
ClassNamesProperty
} from "./defs"

export {}
describe("ClassNamesProperty", () => {
it("Free declaration", () => {
type Props = ClassNamesProperty<{
class1: ClassHash, class2: ClassHash
}>
const suites: Record<string, Props["classnames"]> = {
"all setted": {
class1: "class1",
class2: undefined
},
"redundant": {
class1: "class1",
class2: undefined,
//@ts-expect-error
redundant: "redundant"
},
//@ts-expect-error
"missed": {
class1: "class1"
},
"wrong type": {
class1: "class1",
//@ts-expect-error
class2: false
}
}
expect(suites).toBeInstanceOf(Object)
})
it("Module based", () => {
type CssModule = {
App: ClassHash
class1: ClassHash, class2: ClassHash
}

type Getter<T extends ReactRelated> = GetProps<T>
type Props = ClassNamesProperty<CssModule, {
class1: ClassHash
class2: ClassHash
//TODO #12 Why no suggestion?
}>

type PropsWithWrong = ClassNamesProperty<CssModule,
//@ts-expect-error
{class3: ClassHash}
>

const suite4wrong: PropsWithWrong["classnames"] = {
class3: undefined,
},
suite: Props["classnames"] = {
class1: "class1",
class2: undefined
}
expect({suite4wrong, suite}).toBeInstanceOf(Object)
})
})

it.todo("ClassNamesFrom")

describe("ReactRelated", () => {
type Getter<T extends ReactRelated> = GetProps<T>

type Without = ClassNamed
type Wrong = ClassNamed & {classnames: string}
type Some = ClassNamed & {classnames: Record<string, ClassHash>}
Expand Down Expand Up @@ -64,4 +124,4 @@ describe("ReactRelated", () => {
})


})
})
11 changes: 6 additions & 5 deletions src/defs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type ClassNames<
C10 extends ReactRelated = never
>
= Ever<Extract<C0, true>, ClassNamed>
& ClassNamesProperty<
& ClassNamesCombiner<
& Ever<Extract<C0, ReactRelated>, ClassNamesFrom<Extract<C0, ReactRelated>>>
& Ever<C1, ClassNamesFrom<C1>>
& Ever<C2, ClassNamesFrom<C2>>
Expand All @@ -39,8 +39,10 @@ export type ClassNames<
& Ever<C10, ClassNamesFrom<C10>>
>

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

export type ClassHash = undefined|string

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

/// iNTERNAL

type WithClassNames = ClassNamesProperty<CssModule>
type WithClassNames = {classnames: CssModule}

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

0 comments on commit 1fe375b

Please sign in to comment.