Skip to content

How do I create subtypes for IDs? #3

Description

@Shinigami92

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'>;

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions