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

Extend Jsonify type to handle Tagged types #1010

Open
marcuspoehls opened this issue Dec 16, 2024 · 0 comments
Open

Extend Jsonify type to handle Tagged types #1010

marcuspoehls opened this issue Dec 16, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@marcuspoehls
Copy link

marcuspoehls commented Dec 16, 2024

I’m using Tagged types in combination with Jsonify-ed types. It seems that tagged types aren’t unwrapped when creating their JSON type.

Am I missing something or is that an option for improvement?

Here’s the sample code that I’m using:

import type { Tagged, Jsonify } from 'type-fest'

export type UserId = Tagged<number, 'UserId'>

class User extends BaseModel {
  declare id: UserId
}

// then I’m extracting the DTO and DTO-JSON types

export interface UserDto {
  id: User['id']
}

export type UserDtoJson = Jsonify<UserDto>
// type UserDtoJson = {
//     id: UserId; ⚠️ still the tagged type, not the actual type
// }

I was hoping the UserDtoJson type would have the following shape:

type UserDtoJson = {
    id: number;
}

Using Unwrapped for the DTO type resolve the expected, unwrapped type from the tagged type:

import type { UnwrapTagged, Jsonify } from 'type-fest'

export interface UserDto {
  id: UnwrapTagged<User['id']>
}

export type UserDtoJson = Jsonify<UserDto>
// type UserDtoJson = {
//     id: number; ✅ the expected result
// }

Is extending the Jsonify type an option to resolve tagged types and unwrap them automatically?

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@marcuspoehls marcuspoehls added the enhancement New feature or request label Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant