Skip to content

Commit

Permalink
Fix reusable conditions (#62)
Browse files Browse the repository at this point in the history
* Fix typing for complex reusable conditions.

* add changeset
  • Loading branch information
nsaunders authored Jul 6, 2024
1 parent a17ec2e commit 68a6480
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changeset/fuzzy-dogs-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@embellish/core": patch
"@embellish/react": patch
---

Fixed typing for complex reusable conditions.
6 changes: 3 additions & 3 deletions docs/api/react.createconditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Creates reusable conditions based on the provided hooks, each consisting of eith
**Signature:**

```typescript
export declare function createConditions<AvailableHooks extends Hooks<Selector>, ConditionName extends string, S extends keyof AvailableHooks = AvailableHooks extends Hooks<infer S> ? S : never>(hooks: AvailableHooks, conditions: {
export declare function createConditions<S extends Selector, ConditionName extends string>(hooks: Hooks<S>, conditions: Record<string, Condition<S>> & {
[Name in ConditionName]: ValidConditionName<Name> & Condition<S>;
}): Conditions<ConditionName>;
```
Expand Down Expand Up @@ -39,7 +39,7 @@ hooks

</td><td>

AvailableHooks
[Hooks](./react.hooks.md)<!-- -->&lt;S&gt;


</td><td>
Expand All @@ -55,7 +55,7 @@ conditions

</td><td>

{ \[Name in ConditionName\]: [ValidConditionName](./react.validconditionname.md)<!-- -->&lt;Name&gt; &amp; [Condition](./react.condition.md)<!-- -->&lt;S&gt;; }
Record&lt;string, [Condition](./react.condition.md)<!-- -->&lt;S&gt;&gt; &amp; { \[Name in ConditionName\]: [ValidConditionName](./react.validconditionname.md)<!-- -->&lt;Name&gt; &amp; [Condition](./react.condition.md)<!-- -->&lt;S&gt;; }


</td><td>
Expand Down
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"module": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"prepare": "tsc"
"prepare": "tsc",
"test": "tsc --project test"
},
"exports": {
".": {
Expand Down
12 changes: 4 additions & 8 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,9 @@ export type Conditions<ConditionName extends string> = Branded<
* either a hook id or a logical combination using the `and`, `or`, and `not`
* operators.
*
* @typeParam AvailableHooks - The type of hooks available for use in conditions
* @typeParam ConditionName - The type of the names of conditions to create
* @typeParam S - The selector logic of the hooks available for use in
* conditions
* @typeParam ConditionName - The type of the names of conditions to create
* @param hooks - The hooks available for use in conditions
* @param conditions - The conditions to create
*
Expand All @@ -181,14 +180,11 @@ export type Conditions<ConditionName extends string> = Branded<
* @public
*/
export function createConditions<
AvailableHooks extends Hooks<Selector>,
S extends Selector,
ConditionName extends string,
S extends keyof AvailableHooks = AvailableHooks extends Hooks<infer S>
? S
: never,
>(
hooks: AvailableHooks,
conditions: {
hooks: Hooks<S>,
conditions: Record<string, Condition<S>> & {
[Name in ConditionName]: ValidConditionName<Name> & Condition<S>;
},
) {
Expand Down
16 changes: 16 additions & 0 deletions packages/core/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { createConditions, createHooks } from "../src/index.js";

// type-level tests

() => {
// complex reusable conditions
const { hooks } = createHooks([
"@media (prefers-color-scheme: dark)",
"&:hover",
"&.active",
]);
createConditions(hooks, {
dark: "@media (prefers-color-scheme: dark)",
hoverUnselected: { and: ["&:hover", { not: "&.active" }] },
});
};
7 changes: 7 additions & 0 deletions packages/core/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"noEmit": true
},
"include": ["."]
}

0 comments on commit 68a6480

Please sign in to comment.