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: constant and maybe types #36
feat: constant and maybe types #36
Conversation
} | ||
|
||
describe(){ | ||
return this.value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return this.name? (stringified)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is done to make typescript-like describes. TypeScript itself shows "a" | "b" | "c", so maybe it should be JSON.stringify(this.value)
} | ||
|
||
is(value) { | ||
return value === this.value && super.is(value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does the super.is
add?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just the isPrimitive check, we can remove the extends Primitive if you want
} | ||
|
||
export function createConstantFactory(value: any): any { | ||
invariant(isPrimitive(value), 'Constant types can be built only on top of primitives') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Future work could be to support (plain) complex types and deep Freeze?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I inteded to do it in a separate type named "forzen" or "serializable", the problem with complex object is if the equality check should be done on reference or deep equals.
import {isFactory, IFactory} from "../core/factories" | ||
|
||
export function createMaybeFactory(type: IFactory<any, any>): IFactory<any, any>{ | ||
return createDefaultValueFactory(createUnionFactory(createConstantFactory(null), type), null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Singleton for null factory?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense!
Great! |
Builds on top of #35