Better, Faster, Stronger...generics
Changes the object generics to store the derived type instead of the object shape. This imposes a few limitations on type accuracy at the edges, but dramatically speeds up the type processing by tsc on the command line and in editor. It also removes the need for the SchemaOf helper which worked...poorly.
Instead the ObjectSchema class accepts a plain type as its first generic:
interface Folder {
id: ObjectId,
label: string,
files?: File[]
}
- const folder: SchemaOf<Folder, ObjectId | File> = object({
- id: mixed<ObjectId>().defined(),
- label: string().defined(),
- files: array(mixed<File>().defined())
- })
+ const folder: ObjectSchema<Folder> = object({
+ id: mixed<ObjectId>().defined(),
+ label: string().defined(),
+ files: array(mixed<File>().defined())
+ })
It's a small diff, but big improvement in type accuracy and usability, especially with custom schema for class instances.
Note that the generics on the
object()factory method are still the "object shape, meaningobject<Folder>()won't work as expected. This is a compromise between the two strategies for handling generics and allows for an accurate type on