-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add type utils folder
- Loading branch information
Showing
7 changed files
with
194 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { WritableKeys } from '.' | ||
import type { CollectionRef, EntityByMetaType, MetaType } from '..' | ||
import type { Entity } from '../Entity' | ||
import type { EntityRef } from '../EntityRef' | ||
|
||
// prettier-ignore | ||
|
||
export type Patch< | ||
T extends Entity<U> | Entity<U>[], | ||
U extends MetaType = MetaType | ||
> = | ||
// Patch<Entity> | ||
T extends Entity<U> | ||
? Partial< | ||
{ | ||
[P in WritableKeys<T>]: | ||
// Коллекция МойСклад (positions) | ||
T[P] extends CollectionRef<infer M> | undefined | ||
? (Patch<EntityByMetaType[M]> & EntityRef<M>)[] | undefined | ||
|
||
// Массив сущностей (attributes) | ||
: T[P] extends Array<Entity<infer M>> | undefined | ||
? (Patch<EntityByMetaType[M]> & EntityRef<M>)[] | undefined | ||
|
||
// Default | ||
: T[P] | ||
} | ||
> | ||
|
||
// Patch<Entity[]> -> | ||
: T extends Array<infer M> | ||
// -> Entity | ||
? M extends Entity<U> | ||
? Array<Patch<M> & EntityRef<U>> | ||
: never | ||
|
||
: never |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export * from './Expand' | ||
export * from './Patch' | ||
|
||
// https://stackoverflow.com/questions/49579094/typescript-conditional-types-filter-out-readonly-properties-pick-only-requir | ||
|
||
export type IfEquals<X, Y, A = X, B = never> = (<T>() => T extends X | ||
? 1 | ||
: 2) extends <T>() => T extends Y ? 1 : 2 | ||
? A | ||
: B | ||
|
||
export type ReadonlyKeys<T> = { | ||
[P in keyof T]-?: IfEquals< | ||
{ [Q in P]: T[P] }, | ||
{ -readonly [Q in P]: T[P] }, | ||
never, | ||
P | ||
> | ||
}[keyof T] | ||
|
||
export type WritableKeys<T> = { | ||
[P in keyof T]-?: IfEquals< | ||
{ [Q in P]: T[P] }, | ||
{ -readonly [Q in P]: T[P] }, | ||
P | ||
> | ||
}[keyof T] | ||
|
||
// https://medium.com/@KevinBGreene/typescript-modeling-required-fields-with-mapped-types-f7bf17688786 | ||
export type RequireKeys<T, K extends keyof T> = { | ||
[X in Exclude<keyof T, K>]?: T[X] | ||
} & | ||
{ | ||
[P in K]-?: T[P] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { Attribute, CustomerOrder, MetaType, Patch } from '../../../src' | ||
|
||
//#region Патч объекта | ||
const t1 = {} as Patch<CustomerOrder> | ||
|
||
// @ts-expect-error | ||
t1.id // should omit readonly fields | ||
|
||
// @ts-expect-error | ||
t1.name.toString() // should make fields optional | ||
|
||
t1.name?.toString() | ||
|
||
t1.positions = [ | ||
{ | ||
meta: { type: MetaType.CustomerOrderPosition, href: '' }, | ||
price: 12300, | ||
discount: 10 | ||
} | ||
] | ||
|
||
t1.positions = [ | ||
// @ts-expect-error | ||
{ | ||
// У элемента коллекции должен быть обязательно указан meta | ||
// meta? | ||
price: 12300, | ||
discount: 10 | ||
} | ||
] | ||
|
||
t1.attributes = [ | ||
{ | ||
meta: { | ||
type: MetaType.AttributeMetadata, | ||
href: '' | ||
}, | ||
value: 123 | ||
} | ||
] | ||
|
||
t1.attributes = [ | ||
{ | ||
meta: { | ||
type: MetaType.AttributeMetadata, | ||
href: '' | ||
}, | ||
value: '' | ||
} | ||
] | ||
|
||
t1.attributes = [ | ||
// @ts-expect-error | ||
{ | ||
value: '' | ||
} | ||
] | ||
|
||
t1.attributes = [ | ||
{ | ||
meta: { | ||
type: MetaType.AttributeMetadata, | ||
href: '' | ||
}, | ||
// @ts-expect-error | ||
name: '', | ||
value: '' | ||
} | ||
] | ||
//#endregion | ||
|
||
//#region Патч коллекции | ||
const t4: Patch<CustomerOrder[]> = [ | ||
{ | ||
meta: { | ||
type: MetaType.CustomerOrder, | ||
href: '' | ||
}, | ||
name: 'foo', | ||
attributes: [ | ||
{ | ||
meta: { | ||
type: MetaType.AttributeMetadata, | ||
href: '' | ||
} | ||
} | ||
], | ||
positions: [ | ||
{ | ||
meta: { type: MetaType.CustomerOrderPosition, href: '' }, | ||
price: 45, | ||
discount: 10 | ||
} | ||
] | ||
} | ||
] | ||
//#endregion | ||
|
||
//#region Патч вложенной коллекции МойСклад | ||
const attribute1: Patch<Attribute> = { value: 42 } | ||
|
||
const attribute2: Patch<Attribute[]> = [ | ||
{ | ||
meta: { | ||
href: '', | ||
type: MetaType.AttributeMetadata | ||
}, | ||
value: 42 | ||
} | ||
] | ||
//#endregion |