fix(common): emit type errors for optional fields when WasValidated is true #2213
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #2182
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
When using
ConfigService<Config, true>with a configuration type that includes optional fields (e.g., from Zod's.optional()), TypeScript does not emit type errors when assigningstring | undefinedto a strictstringtype.This happens because the
Rtype parameter in theget/getOrThrowoverloads is inferred from contextual typing (the expected return type) instead of using the defaultPathValue<T, P>.Example:
Issue Number: #2182
What is the new behavior?
TypeScript now correctly emits type errors when assigning potentially undefined values to strict types, regardless of the
WasValidatedparameter.The fix removes the
Rtype parameter from the affected overloads and usesPathValue<T, P>directly in the return type, preventing contextual typing from overriding the correct type inference.Does this PR introduce a breaking change?
The fix is compile-time only and does not change runtime behavior. Code that previously compiled without errors but was incorrectly typed will now show appropriate type errors, which is the expected/correct behavior.
Other information
Affected method overloads:
get(propertyPath, options)get(propertyPath, defaultValue, options)getOrThrow(propertyPath, options)getOrThrow(propertyPath, defaultValue, options)Added test file
tests/e2e/type-inference-validated.spec.tsthat uses@ts-expect-errorto verify correct type inference behavior for bothWasValidated = trueandWasValidated = falsecases