Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add object arrays #274

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/use-replicant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { klona as clone } from "klona/json";

type JsonValue = boolean | number | string | null;

type Json = JsonValue | JsonValue[] | { [key: string]: Json };
type Json = JsonValue | JsonValue[] | { [key: string]: Json } | Json[];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I suggest simply importing and using the Jsonifiable type from the type-fest package? That will cover other edge cases for you like objects with a toJson method, and read-only arrays which both would be rejected by this type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally and the Jsonifiable type works. @Hoishin thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding package just for this simple type is overkill for me. Edge cases you mentioned don't happen in this case.


export type UseReplicantOptions<T> = {
defaultValue?: T;
Expand Down
8 changes: 7 additions & 1 deletion tests/use-replicant.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,16 @@ interface RunnerNameProps {
prefix?: string;
}

type RunnerNameReplicant = {
runner: {
name: string;
};
};

const RunnerName: React.FC<RunnerNameProps> = (props) => {
const { prefix } = props;
const repName = `${prefix ?? "default"}:currentRun`;
const [currentRun] = useReplicant(repName, {
const [currentRun] = useReplicant<RunnerNameReplicant>(repName, {
defaultValue: { runner: { name: "foo" } },
});
if (!currentRun) {
Expand Down
Loading