You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using the factory overrides to set a property explicitly to undefined this is always allowed even when the type constraint doesn’t accept undefined as a valid value.
To Reproduce
import{Factory}from'fishery';interfaceFoo{bar: string;}constfooFactory=Factory.define<Foo>(()=>{return{bar: 'any-value'};});constresult=fooFactory.build({bar: undefined});// result has the type Foo but its value is { bar: undefined } which is not a valid Foo
Additional context
It seems like this bug has been introduced with #44
The text was updated successfully, but these errors were encountered:
Thank you for reporting this issue, and apologies for the delay in getting back. This does seem to be an issue. Ensuring that factories are type-safe is more important than allowing users to un-set a property, which is what the motivation behind #44. I am leaning right now toward reverting #44 and releasing it as a major version bump.
Currently, all params passed to build are optional, and TypeScript by default allows explicitly passing undefined for properties that are typed as optional (eg. bar?: string). Ideally, TypeScript would only allow omitting the property but not passing undefined, so that fooFactory.build({ bar: undefined }) would produce a type error. This behavior is only possible with a TypeScript compiler options flag, exactOptionalPropertyTypes, which most users will not have set.
My team uses the factory to produce values for undefined properties and produce objects with no optional properties. Today I lost valuable time troubleshooting failing tests because a build function was returning an incomplete object due to this issue.
There isn't an obvious distinction between an explicitly undefined value and an implicit undefined from omission and IMO this is just too surprising and error-prone:
Description
When using the factory overrides to set a property explicitly to
undefined
this is always allowed even when the type constraint doesn’t acceptundefined
as a valid value.To Reproduce
Additional context
It seems like this bug has been introduced with #44
The text was updated successfully, but these errors were encountered: