question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with recurisve parsing and typescript

See original GitHub issue

So I have the following setup for parsing a recursive rules tree

interface RuleNode {
    ruleId: string;
    description?: string[];
    children: RuleNode[];
    ruleOperation: RuleOperation;
    ruleField?: string;
    ruleValue?: string;
    ruleValueType?: RuleValueType;
}
export type RuleSyntaxTree = RuleNode;

export const RuleNode: z.ZodType<RuleNode> = z.lazy(() =>
    z.object({
        ruleId: z.string(),
        description: z.array(z.string()).optional(),
        children: z.array(RuleNode),
        ruleOperation: z.nativeEnum(RuleOperationInternal),
        ruleValue: z.string().optional(),
        ruleField: z.string().optional(),
        ruleValueType: z.string().optional(),
    }),
);

This gives me the following error on the validator.

Type 'ZodLazy<ZodObject<{ ruleId: ZodString; description: ZodOptional<ZodArray<ZodString, "many">>; children: ZodArray<ZodType<RuleNode, ZodTypeDef, RuleNode>, "many">; ruleOperation: ZodNativeEnum<...>; ruleValue: ZodOptional<...>; ruleField: ZodOptional<...>; ruleValueType: ZodOptional<...>; }, "strip", ZodTypeAny, { .....' is not assignable to type 'ZodType<RuleNode, ZodTypeDef, RuleNode>'.
  Types of property '_type' are incompatible.
    Type '{ ruleId?: string; description?: string[]; children?: RuleNode[]; ruleOperation?: RuleOperation; ruleValue?: string; ruleField?: string; ruleValueType?: string; }' is not assignable to type 'RuleNode'.
      Property 'ruleId' is optional in type '{ ruleId?: string; description?: string[]; children?: RuleNode[]; ruleOperation?: RuleOperation; ruleValue?: string; ruleField?: string; ruleValueType?: string; }' but required in type 'RuleNode'.t

is there something im doing wrong? Can this code be simplified.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
lemieszcommented, Sep 29, 2022

Okay after further debugging I think I thought I had turned strict mode on… but on second look it was turned of.

Enabling strict mode seems to have solved my problem

0reactions
lemieszcommented, Sep 29, 2022

So I have create a brand new workspace, and I still get the same error. If I just make everything optional then it does actually work. Although obviously I dont want to do this as its not the correct type

enum RuleValueType {
  NUMBER,
}
interface RuleNode {
  ruleId?: string;
  description?: string[];
  children?: RuleNode[];
  ruleOperation?: 'AND';
  ruleField?: string;
  ruleValue?: string;
  ruleValueType?: RuleValueType;
}
export type RuleSyntaxTree = RuleNode;

// Cant figure out the typing here. However this works and is recommended by the Zod library
export const RuleNode: z.ZodType<RuleNode> = z.lazy(() =>
  z.object({
    ruleId: z.string().optional(),
    description: z.array(z.string()).optional(),
    children: z.array(RuleNode).optional(),
    ruleOperation: z.enum(['AND']).optional(),
    ruleValue: z.string().optional(),
    ruleField: z.string().optional(),
    ruleValueType: z.nativeEnum(RuleValueType),
  })
);

using “zod”: “^3.19.1”

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typing a recursive descent parser in TypeScript - Reddit
Hi, I'm building a C compiler in TypeScript. Picked typescript because I was knew to compilers and wanted some language that I was...
Read more >
recursive type definitions · Issue #3496 · microsoft/TypeScript
I have a recursive type definition for JSON Serializable entities (Exhibit A, edited after discussion with @JsonFreeman ): export type ...
Read more >
How do I recursively extract the elements of my expression ...
I write a parser with Chevrotain in Typescript.
Read more >
You are not logged in. Reading 19: Parsers
Recursive abstract data types are often used to represent expressions in a language, like HTML, Markdown, TypeScript, or algebraic expressions (the symbolic ...
Read more >
Casual Parsing in JavaScript August 16, 2021 - brandons.me
A parser, generally, moves through a string of source code and assembles an object-tree by matching the rules in a grammar. One final...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found