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

How do I create subtypes for IDs? #3

Open
Shinigami92 opened this issue Feb 15, 2022 · 0 comments
Open

How do I create subtypes for IDs? #3

Shinigami92 opened this issue Feb 15, 2022 · 0 comments

Comments

@Shinigami92
Copy link

I need some guidance / docs about how to properly extend from a branded ID

// common.ts
import type { Flavor } from 'nom-ts';

export type NodeId = Flavor<string, 'NodeId'>;

export interface Node<Id extends NodeId> {
  id: Id;
  comment?: string;
  createdAt: string;
}

export interface Edge<
  NodeClassId extends NodeId,
  NodeClass extends Node<NodeClassId>,
> {
  node: NodeClass;
  createdAt: string;
}

// -----------------------------------------------------------------------

// cat.ts
import type { Flavor } from 'nom-ts';
import type { Node, NodeId } from './common';

export type CatId = Flavor<NodeId, 'CatId'>;
export type PersonId = Flavor<NodeId, 'PersonId'>;

const cid: CatId = 'a' as CatId;
const lid: PersonId = 'b' as PersonId;

const lid2: PersonId = cid; // No error :(

export interface Cat extends Node<CatId> {
  name: string;
  ownerId: PersonId;
}

export type CatCreate = Omit<Cat, 'id' | 'createdAt'>;
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

No branches or pull requests

1 participant