-
Notifications
You must be signed in to change notification settings - Fork 20
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
Simplified usage for type guards #14
Comments
So you propose that instead of throwing an error: async (req, res) => {
try {
// transform and validate request body
const userObject = await transformAndValidate(User, req.body);
// infered type of userObject is User
// you can access all class prototype properties and methods
} catch (error) {
// your error handling
console.err(error);
}
} We can return boolean that indicates if the value object passed validation: async (req, res) => {
if (isValid(User, req.body)) {
// infered type of req.body is User
// but you can't access all class prototype properties and methods
} else {
// your error handling
console.err(`Invalid body value: ${req.body}`);
}
} The problem is that it won't transform the object into an instance of the class (like |
It's not a replacement for the current functionality. I agree that there are many situations where the full spectrum is needed. But there might also be those where a slimmed down version is all you need and could make the code more readable, specifically if the class is a replacement for a simple interface. E. g. let's say I have a configuration object enum LogLevel {
error: 'error';
info: 'info';
}
class LoggerOptions {
@IsEnum(LogLevel)
level: LogLevel
}
const isLoggerOptions = isValidFactory(LoggerOptions) then I could write
To be honest I am not 100% it is worth it myself, so we could also arrive at the conclusion this functionality doesn't belong in this package :) |
I mean replacement in your app code, not in this library. We can export more functions without a problem 😉 The use case is nice but I would expose only a |
We've worked more on our codebase and came to the realization that for us it is fine to always use errors instead of books and for type checking, so from my side this issue could be closed. |
@dbartholomae We can make it typesafe by using conditional types and skipping the function properties. |
I'm currently using this package with a helper to create type guards:
Would you be interested in a PR that adds this helper?
The text was updated successfully, but these errors were encountered: