Integrating experimental redux-orm typings with create-react-app@^3.0.0
npm installnpm start
warning: these types are a WIP - likely to change in future
Substitute @types/redux-orm dependency with experimental redux-orm types branch:
{
//...
"dependencies": {
//...
"@types/redux-orm": "github:tomasz-zablocki/redux-orm-typings#experimental"
}
}Do not declare instance properties inside class definitions - babel transpiles them into additional prototype properties which interfere with redux-orm descriptors.
- each
Modelis defined by a class and an interface with matching identifiers - define an interface describing the shape of
Model's instances:interface SourceModel { id: number name: string target: TargetModel } interface TargetModel { id: number sources: QuerySet<SourceModel> }
- define related
Modelclass extendingModelproviding additional type parameter.
Set the parameter totypeof (TheModelBeingDefined)class SourceModel extends Model<typeof SourceModel> { static modelName = 'SourceModel' as const static fields = { id: attr(), name: attr(), target: fk('TargetModel', 'sources') } } class TargetModel extends Model<typeof TargetModel> { static modelName = 'TargetModel' as const static fields = { id: attr() } }
Assuming two related Model classes: SourceModel and TargetModel
SourceModel.fields |
interface SourceModel |
interface TargetModel |
|---|---|---|
attr() |
string, number, boolean, plain objects, arrays of primitives/plain objects |
|
fk('TargetModel') |
TargetModel |
QuerySet<SourceModel> |
oneToOne('TargetModel') |
TargetModel |
SourceModel |
many('TargetModel') |
MutableQuerySet<TargetModel> |
MutableQuerySet<SourceModel> |