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

feat: validation error type #13

Merged
merged 3 commits into from
Oct 7, 2023
Merged

Conversation

drochetti
Copy link
Collaborator

No description provided.

@@ -33,3 +33,9 @@ export type QueueStatus =
export function isQueueStatus(obj: any): obj is QueueStatus {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have to be ambiguous?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a type-predicate (formerly known as type-guard). So the function returns true/false during the runtime, but in build-time it gives the compiler information about the type, so:

const someUntypedJsonComingFromARequest = /* ... */
if (isQueueStatus(someUntypedJsonComingFromARequest)) {
  // now the compiler knows that someUntypedJsonComingFromARequest  is actually QueueStatus
}

https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates


Does that clarify it?

@drochetti drochetti merged commit f240e62 into main Oct 7, 2023
1 check passed
@drochetti drochetti deleted the feature/validation-error-type branch October 8, 2023 08:46
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

Successfully merging this pull request may close these issues.

2 participants