mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-12-06 13:11:46 +00:00
[@types/redux-orm] fix overly broad IdOrModelLike type to match library code (#36167)
This commit is contained in:
committed by
Ron Buckton
parent
b87418f945
commit
39cd3fcb94
Vendored
+5
-12
@@ -2,7 +2,6 @@ import { ModelTableOpts, TableOpts } from './db';
|
||||
import { IdAttribute } from './db/Table';
|
||||
import { AttributeWithDefault, FieldSpecMap, ForeignKey, ManyToMany, OneToOne } from './fields';
|
||||
import { Optional, OptionalKeys, Overwrite, PickByValue } from './helpers';
|
||||
import { IdOrModelLike, ModelField } from './index';
|
||||
import QuerySet, { LookupSpec, MutableQuerySet, SortIteratee, SortOrder } from './QuerySet';
|
||||
import { OrmSession } from './Session';
|
||||
|
||||
@@ -50,10 +49,9 @@ export interface ModelFieldMap {
|
||||
/**
|
||||
* A Model-derived mapped type for supplying relations and alike.
|
||||
*
|
||||
* Either a primitive type matching Model's identifier type or a map containing an {IdAttribute: IdType} pair,
|
||||
* where IdAttribute and IdType match respective Model property key and type
|
||||
* Either a primitive type matching Model's identifier type or an object implementing a `{ getId(): IdType<M> }` interface
|
||||
*/
|
||||
export type IdOrModelLike<M extends Model> = IdType<M> | IdEntry<M>;
|
||||
export type IdOrModelLike<M extends Model> = IdType<M> | {getId(): IdType<M>; };
|
||||
|
||||
/**
|
||||
* The heart of an ORM, the data model.
|
||||
@@ -300,7 +298,7 @@ export default class Model<MClass extends typeof AnyModel = any, Fields extends
|
||||
* Gets the id value of the current instance by looking up the id attribute.
|
||||
* @return The id value of the current instance.
|
||||
*/
|
||||
getId(): string | number;
|
||||
getId<Id extends Fields[IdAttribute<MClass>] = Fields[IdAttribute<MClass>]>(): Id extends undefined ? number: Id;
|
||||
|
||||
/**
|
||||
* @return A string representation of this {@link Model} instance.
|
||||
@@ -327,7 +325,7 @@ export default class Model<MClass extends typeof AnyModel = any, Fields extends
|
||||
* @param propertyName - name of the property to set
|
||||
* @param value - value assigned to the property
|
||||
*/
|
||||
set<K extends string>(propertyName: K, value: RefPropOrSimple<InstanceType<MClass>, K>): void;
|
||||
set<K extends string>(propertyName: K, value: RefPropOrSimple<this, K>): void;
|
||||
|
||||
/**
|
||||
* Assigns multiple fields and corresponding values to this {@link Model} instance.
|
||||
@@ -361,7 +359,7 @@ export class AnyModel extends Model {}
|
||||
* Relations can be provided in a flexible manner for both many-to-many and foreign key associations
|
||||
* @see {@link IdOrModelLike}
|
||||
*/
|
||||
export type UpsertProps<M extends Model> = Overwrite<Partial<CreateProps<M>>, Required<IdEntry<M>>>;
|
||||
export type UpsertProps<M extends Model> = Overwrite<Partial<CreateProps<M>>, {[K in IdKey<M>]-?: IdType<M>}>;
|
||||
|
||||
/**
|
||||
* {@link Model#update} argument type
|
||||
@@ -401,11 +399,6 @@ export type IdType<M extends Model> = IdKey<M> extends infer U
|
||||
: number
|
||||
: number;
|
||||
|
||||
/**
|
||||
* A single entry map representing IdKey: IdType property of supplied {@link Model}.
|
||||
*/
|
||||
export type IdEntry<M extends Model> = { [K in IdKey<M>]: IdType<M> };
|
||||
|
||||
/**
|
||||
* Type of {@link Model.ref} / database entry for a particular Model type
|
||||
*/
|
||||
|
||||
Vendored
+2
@@ -249,6 +249,8 @@ export default class QuerySet<M extends AnyModel = any, InstanceProps extends ob
|
||||
|
||||
/**
|
||||
* {@link QuerySet} extensions available on {@link ManyToMany} fields of session bound {@link Model} instances.
|
||||
*
|
||||
* @see {@link IdOrModelLike}
|
||||
*/
|
||||
export interface ManyToManyExtensions<M extends AnyModel> {
|
||||
add: (...entitiesToAdd: ReadonlyArray<IdOrModelLike<M>>) => void;
|
||||
|
||||
@@ -171,20 +171,17 @@ const argPropertyTypeRestrictionsOnCreate = () => {
|
||||
* Properties associated to relational fields may be supplied with:
|
||||
*
|
||||
* - a primitive type matching id type of relation target
|
||||
* - a Ref type derived from relation target
|
||||
* - Model/SessionBoundModel instance matching relation target
|
||||
* - a map containing {Idkey:IdType} entry, where IdKey/IdType are compatible with relation target id key:type signature
|
||||
*
|
||||
* In case of MutableQuerySets/many-to-many relationships, an array of union of above-mentioned types is accepted
|
||||
*/
|
||||
const authorModel = Person.create({ id: 'A1', firstName: 'A1', lastName: 'A1' });
|
||||
const publisherModel = Publisher.create({ name: 'P1' });
|
||||
Book.create({ title: 'B1', publisher: publisherModel, authors: [authorModel] });
|
||||
Book.create({ title: 'B1', publisher: publisherModel.ref, authors: [authorModel.ref] });
|
||||
Book.create({
|
||||
title: 'B1',
|
||||
publisher: { index: publisherModel.index },
|
||||
authors: [{ id: authorModel.id }, 'A1', authorModel, authorModel.ref]
|
||||
publisher: publisherModel.index ,
|
||||
authors: [authorModel, 'A1', authorModel, authorModel.ref.id]
|
||||
});
|
||||
|
||||
/** Id types are verified to match relation target */
|
||||
@@ -223,13 +220,8 @@ const argPropertyTypeRestrictionsOnUpsert = () => {
|
||||
*/
|
||||
const authorModel = Person.upsert({ id: 'A1', firstName: 'A1', lastName: 'A1' });
|
||||
const publisherModel = Publisher.upsert({ name: 'P1', index: 1 });
|
||||
Book.upsert({ title: 'B1', publisher: 1, authors: [authorModel] });
|
||||
Book.upsert({ title: 'B1', publisher: publisherModel, authors: [authorModel] });
|
||||
Book.upsert({ title: 'B1', publisher: publisherModel.ref, authors: [authorModel.ref] });
|
||||
Book.upsert({
|
||||
title: 'B1',
|
||||
publisher: { index: publisherModel.index },
|
||||
authors: [{ id: authorModel.id }, 'A1', authorModel, authorModel.ref]
|
||||
});
|
||||
|
||||
/** Id types are verified to match relation target */
|
||||
Book.create({ title: 'B1', publisher: authorModel }); // $ExpectError
|
||||
|
||||
Reference in New Issue
Block a user