Skip to content

Add generic types for Node#labels, Relationship#type and UnboundRelationship#type #1014

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

Merged
merged 1 commit into from
Nov 15, 2022

Conversation

bigmontz
Copy link
Contributor

@bigmontz bigmontz commented Nov 8, 2022

This generic types enable defining a list string like values accepted for an specific node labels list or relationship type.

Node example:

type PersonLabels = 'Person' | 'Actor'

// get the person from a non-typed record. If the record is correctly type-mapped
// the Node type can be omitted since it will be inferred.
const person: Node<Integer, PersonProps, PersonLabels> = record.get('person')

// get labels
// the type inferred for the labels will be PersonLabels[]
const labels = person.labels

// @ts-exepct-error
const wrongLabels: 'Car'|'Bicycle'[] = person.labels

Relationship/UnboundRelationship example:

type ActedInType = 'ACTED_IN'

// get the relationship from a non-typed record. If the record is correctly type mapped
// the Relationship type can be omitted since it will be inferred.
const actedIn: Relationship<Integer, ActedInProps, ActedInType> = record.get('actedIn')

// get type
// the type inferred for the relType will be 'ACTED_IN'
const relType = actedIn.type

// @ts-expect-error
const directed: 'DIRECTED' = actedIn.type

⚠️ This type definitions are not asserted in runtime. Thus mismatched type records coming from the database will not trigger type errors.

…ionship#type

This generic types enable defining a list string like values accepted for an specific node labels list or relationship type.

Node example:

```typescript
type PersonLabels = 'Person' | 'Actor'

// get the person from a non-typed record. If the record is correctly type-mapped
// the Node type can be omitted since it will be inferred.
const person: Node<Integer, PersonProps, PersonLabels> = record.get('person')

// get labels
// the type inferred for the labels will be PersonLabels[]
const labels = person.labels

// @ts-exepct-error
const wrongLables: 'Car'|'Bicycle'[] = person.labels
```

Relationship/UnboundRelationship example:

```typescript
type ActedInType = 'ACTED_IN'

// get the relationship from a non-typed record. If the record is correctly type mapped
// the Relationship type can be omitted since it will be inferred.
const actedIn: Relationship<Integer, ActedInProps, ActedInType> = record.get('actedIn')

// get type
// the type inferred for the relType will be 'ACTED_IN'
const relType = actedIn.type

// @ts-expect-error
const directed: 'DIRECTED' = actedIn.type
```

⚠️ This type definitions are not asserted in runtime. Thus mismatched type records coming from the database will not trigger type errors.
@robsdedude robsdedude self-requested a review November 14, 2022 10:34
Copy link
Member

@robsdedude robsdedude left a comment

Choose a reason for hiding this comment

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

🌟

@bigmontz bigmontz merged commit 85c0939 into neo4j:5.0 Nov 15, 2022
@bigmontz bigmontz deleted the 5.x-type-improvements branch November 15, 2022 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants