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

Support "branded" custom types #654

Closed
jasperlin451 opened this issue Jun 10, 2023 · 5 comments
Closed

Support "branded" custom types #654

jasperlin451 opened this issue Jun 10, 2023 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@jasperlin451
Copy link

Feature Request

Our existing codebase has a variety of existing "Branded" type definitions. For example, we have

type Url = string & {
    __brands: {
        Url: "Url";
    };
}

When creating the assert definitions, this is what I see:

export const assertUrl = (input: any): Url => {
    const __is = (input: any): input is Url => {
        return true;
    };
    if (false === __is(input))
        ((input: any, _path: string, _exceptionable: boolean = true): input is Url => {
            return true;
        })(input, "$input", true);
    return input;
};
export const assertString = (input: any): string => {
    const __is = (input: any): input is string => {
        return "string" === typeof input;
    };
    if (false === __is(input))
        ((input: any, _path: string, _exceptionable: boolean = true): input is string => {
            const $guard = (typia.createAssert as any).guard;
            return "string" === typeof input || $guard(true, {
                path: _path + "",
                expected: "string",
                value: input
            });
        })(input, "$input", true);
    return input;
};

Feature requests:

  1. It would be ideal to get a warning message about types Typia doesn't understand. Right now it just assumes everything is a Url. Maybe Typia assumes you know you're only supposed to use basic types with it but you have to read the generated output to understand that.
  2. Is there a way to support these branded types? I can understand that it would be hard to provide a general solution but if typia could support the user passing in some mapping (e.g. Url -> string)
@samchon samchon self-assigned this Jun 10, 2023
@samchon samchon added the bug Something isn't working label Jun 10, 2023
@samchon
Copy link
Owner

samchon commented Jun 10, 2023

I need to study about it, but may consume lots of time.

@samchon
Copy link
Owner

samchon commented Jun 10, 2023

By the way, such crazy type comes from a specific library? If that, I also need to study about that for testing.

@samchon
Copy link
Owner

samchon commented Jun 10, 2023

At first, I will ignore intersection type when an object type being used with atomic type like string.

In that case, only string type would be left. @jasperlin451 Do you agree? Then I'll patch in this week.

@jasperlin451
Copy link
Author

The actual implementation of the weird object type is internal to our repo but I think the general concept of "Typescript brands" general involves the intersection type of those base types with some object structure.

As yes, simplifying those "nonsense" intersections down to the string or number type would be a great start.

samchon added a commit that referenced this issue Jun 11, 2023
Fix #654 - support intersection type with `atomic & object`
@MatAtBread
Copy link

If you stumbled across this issue, I have patched typia to support brands. See #911 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants