Skip to content

Add generic parameter to NodePatternOptions to improve type safety for properties #555

@CharlieDigital

Description

@CharlieDigital

Currently, when using methods that accept NodePatternOptions such as Cypher.Pattern() and .to(), there is no enforcement of types in the key of properties.

By adding a type parameter on these methods and on NodePatternOptions, it is then possible to enforce type safety by using the keyof the type as the key of the Record

export type NodePatternOptions<T> = {
    labels?: string | string[] | LabelExpr;
    properties?: Record<keyof T, Expr>;
};

Example in TS Playground

Image

Similarly, it would be nice to be able to specify a type on the .related() like:

export const VALID_RELATIONS = [
  'HAS'
] as const

export type ValidRelations = (typeof VALID_RELATIONS)[number]

new Cypher.Pattern(...)
   .related<ValidRelations>({ type: 'HAS'}) 

new Cypher.Pattern(...)
   .related<ValidRelations>({ type: 'NO_LONGER_HAS'}) 

This ensures that when creating nodes, all properties are provided.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions