Issue with recurisve parsing and typescript
See original GitHub issueSo 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:
- Created a year ago
- Comments:5
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
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
using “zod”: “^3.19.1”