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

[BUG] Doesn't seem to handle complex union types #40

Closed
pipex opened this issue Nov 1, 2023 · 2 comments
Closed

[BUG] Doesn't seem to handle complex union types #40

pipex opened this issue Nov 1, 2023 · 2 comments

Comments

@pipex
Copy link

pipex commented Nov 1, 2023

Describe the bug

I'm trying to create a type that can accept either a positive integer or a numeric string, but type validation is not generating the right code even though each side of the union works by itself. Am I doing something wrong?

type ID =  (number & Int & Min<0>) | (string & Matches<'/\d+/'>);

function validate(user: Assert<ID>) {
    // Your code...
}

validate('1234');

Playground link
https://googlefeud.github.io/ts-runtime-checks?code=C4TwDgpgBAkgIlAvFKAKAdgVwLYCMIBOUAZLOsCVALICW6APAAwB8AlFAD5oDOwBdAc0pUAhsADGACwjd6AcgD0AHQAmAagVy2AbgBQugGaZ044DQD26KADcRAGxoqxEVJm6EAXFACC3dwWB6eDYoAG9dFBQFBSgATXNMInFzFQgAOgzdAF99WwcnYBc5AEYAJgBmABY5Vm0gA

Expected behavior

Should compile into something like this

function validate(user) {
     if (typeof user !== "number" || user % 1 !== 0 || user < 0)
         throw new Error("Expected user to be a number, to be an int, to be greater than 0");
     if (typeof user !== "string" || !/d+/.test(user))
        throw new Error("Expected user to be a string, to match /d+/");
 }
@GoogleFeud
Copy link
Owner

Currently it's not possible to use check types in a union, you could try using the Or check

@GoogleFeud
Copy link
Owner

Possible in 0.6.0!

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

2 participants