feat: type inference for check function#355
Conversation
|
Hello everyone, The TypeScript tests are updated and you can play around with them to see what it "feels like" working with the inferred TS types. Note that I did not add support for the Happy to hear about your feedback and let me know if you have questions! Btw, the TypeScript 7 native implementation is in a usable state now and a charm to play with (since it's ~10x faster, everything feels more "instant"). |
|
Hey @icebob, I was wondering if you have any update on this? :) |
icebob-ai
left a comment
There was a problem hiding this comment.
Thanks for this excellent work, @Laurin-W! The type inference system is really well designed — the TypeFromValidationSchema / TypeFromValidationRule utility types handle arrays, multi-rules, objects, enums, class instances, convert mode, and optional/nullable correctly. This is a high-value addition and we'd love to merge it.
However, there are a few things to address before we can merge:
1. Remove unused import
import type { EnumType } from "typescript";This import doesn't appear to be used anywhere in the file, and it would break users who don't have typescript as a dependency (it's only a devDependency for most projects).
2. Separate formatting changes from functional changes
The current diff is ~33K lines, but almost all of it is whitespace reformatting (tab normalization) in index.d.ts. This makes it very hard to review the actual functional changes. Could you please:
- Revert the whitespace-only changes
- Keep only the functional additions (the inference types, the generic parameters, the
ValidationSchemarefactor, etc.)
3. Rebase on current master
Master has changed since this PR was opened (build toolchain migration, new pipe rule). A rebase would resolve potential conflicts and ensure CI runs cleanly.
The core type inference work (~100 lines of utility types) is solid and we're keen to get it merged. Looking forward to the updated PR!
39c979e to
c85c835
Compare
|
Hi @icebob-ai, thanks for your reply and sorry for the formatting issues. I force-pushed a new commit where the formatting changes and unused imports are removed and the branch is rebased on next. Let me know if you need anything else. Best, |
- Add inference types (`TypeFromValidationSchema`, ...) - Validator.compile and validate functions infer allowable type - Update TypeScript tests with`@ts-expect-error` and more specific schema definitions using `as const` and `satisfies`
c85c835 to
d35b43f
Compare
|
Thanks for the updated PR, @Laurin-W! The formatting cleanup and rebase look good. However, I noticed that the type ValidationSchema<T = any> = ValidationSchemaMetaKeys & {
[key in keyof T]: ValidationRule | undefined;
}and it's now replaced with a non-generic Since all current changes on Could you rework this so the Let me know if you have questions or need help with this! |
|
Hi @icebob-ai, thanks for the heads up! |
The PR adds utility types that can infer the TypeScript types from validation schemas.
This is the type inference part that is eventually intended to be used in moleculer to infer the parameter types of action
handlers.I modified the
compile()function of theValidatorso that the returnedcheckfunction expects the TS type that was specified in the schema passed tocompile().If you prefer to not have this implemented with the
check()/compile()function, let me know. We could also just export the utility types for external use (by moleculer).Note that the inference types do not support all features that the validator does. For example,
"email"or"uuid"are interpreted asstring.Also, type inference for validators with the
considerNullAsAValueflag are not implemented (it does not seem to be a very common use case and the complexity would grow. Let me know if you think it should be implemented).This is WIP.The TS unit tests are not yet adapted (they will require// @ts-expect-errorcomments forcheck()calls with invalid objects).