Skip to content

Commit d354a29

Browse files
authored
fix: Add types for fallback strategy (paularmstrong#441)
1 parent ed12f13 commit d354a29

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ declare namespace schema {
22
export type StrategyFunction<T> = (value: any, parent: any, key: string) => T;
33
export type SchemaFunction = (value: any, parent: any, key: string) => string;
44
export type MergeFunction = (entityA: any, entityB: any) => any;
5+
export type FallbackFunction<T> = (key: string, schema: schema.Entity<T>) => T;
56

67
export class Array<T = any> {
78
constructor(definition: Schema<T>, schemaAttribute?: string | SchemaFunction)
@@ -12,6 +13,7 @@ declare namespace schema {
1213
idAttribute?: string | SchemaFunction
1314
mergeStrategy?: MergeFunction
1415
processStrategy?: StrategyFunction<T>
16+
fallbackStrategy?: FallbackFunction<T>
1517
}
1618

1719
export class Entity<T = any> {

typescript-tests/entity.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ type Tweet = {
1414
const data = {
1515
/* ...*/
1616
};
17-
const user = new schema.Entity('users', {}, { idAttribute: 'id_str' });
17+
const user = new schema.Entity<User>(
18+
'users',
19+
{},
20+
{ idAttribute: 'id_str', fallbackStrategy: (key) => ({ id_str: key, name: 'Unknown' }) }
21+
);
1822
const tweet = new schema.Entity(
1923
'tweets',
2024
{ user: user },

0 commit comments

Comments
 (0)