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

Feature request Branded type #1749

Open
MathieuRA opened this issue Feb 17, 2025 · 1 comment
Open

Feature request Branded type #1749

MathieuRA opened this issue Feb 17, 2025 · 1 comment

Comments

@MathieuRA
Copy link

MathieuRA commented Feb 17, 2025

Hi :)

Is there a way to support branded types?

declare const __brand: unique symbol

type Branded<TBrand extends string, TType = string> = TType & { [__brand]: TBrand }

type Foo = {
   id: Branded<'foo'>
   ...
}

The above code outputs something like that:

"Branded_Foo": {
	"allOf": [
		{
			"type": "string"
		},
		{
			"properties": {
				"undefined": {
					"type": "string",
					"enum": [
						"foo"
					],
					"nullable": false
				}
			},
			"required": [
				null
			],
			"type": "object"
		}
	]
},

I also tried replacing Branded types by doing:

type ReplaceBranded<T> = {
  [K in keyof T]: T[K] extends Branded<any> ? string : T[K]
}
ReplaceBranded<Foo>

This works fine with the IDE, but the generated specs convert all properties to string instead of replacing only Branded properties.

EDIT:
I was able to work around the issue by fixing my ReplaceBranded type:

type ReplaceBranded<T> = {
  [K in keyof T]: T[K] extends Branded<string> | undefined
    ? string | undefined
    : T[K] extends Branded<string>[]
      ? string[]
      : T[K] extends Branded<string>
        ? string
        : T[K]
}
Copy link

Hello there MathieuRA 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

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