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

Additional undefined case inside union type #1929

Open
2 tasks done
iy-work opened this issue Jul 14, 2022 · 1 comment
Open
2 tasks done

Additional undefined case inside union type #1929

iy-work opened this issue Jul 14, 2022 · 1 comment
Labels
question Question or help request

Comments

@iy-work
Copy link

iy-work commented Jul 14, 2022

Question

Why is the instance type for types.union(types.string, types.array(types.number)) inferred as string | (number[] & ...) | undefined? I thought that it should be just string | (number[] & ...) without undefined case. Is this behaviour intended or it is a mistake in typings?

playground link

@coolsoftwaretyler
Copy link
Collaborator

Hey @iy-work - sorry it took so long to get back to you!

From my interpretation of the union docs, I think this typing is probably for the case mentioned:

If the correct type cannot be inferred unambiguously from a snapshot, provide a dispatcher function to determine the type

It seems possible that the TypeScript typings for types.union have to account for scenarios where a type cannot be inferred, and a dispatcher function isn't provided (or the function returns undefined).

Another thing that leads me to believe it is that the determineType method declares a return type of possible undefined.

Is that a satisfactory answer for you? Happy to dig in a lil more! If I don't hear back in a week or two, I will probably close out this issue.

@coolsoftwaretyler coolsoftwaretyler added the question Question or help request label Jun 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or help request
Projects
None yet
Development

No branches or pull requests

2 participants