Skip to content

Commit

Permalink
feat(Expand): pass through if expand string is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
wmakeev committed Jul 20, 2021
1 parent 2b02b7b commit dd099ec
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moysklad-api-model",
"version": "0.2.10",
"version": "0.2.11",
"description": "Объектная модель API МойСклад для TypeScript проектов",
"main": "build/src/index.js",
"types": "build/src/index.d.ts",
Expand Down
10 changes: 7 additions & 3 deletions src/model/utils/Expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,20 @@ export type ExpandPath<T, K extends string> =
/**
* Разворачивает поля типа по строке expand в формате API МойСклад
*/
export type Expand<T, U extends string> =
string extends U
export type Expand<T, U extends string | undefined> =
// Исходный тип если expand не задан
U extends undefined
? T

: string extends U
? never

// 'foo.bar,baz'
: U extends `${infer Path},${infer Rest}`
? ExpandPath<T, Path> & Expand<T, Rest>

// 'foo.bar'
: ExpandPath<T, U>
: U extends string ? ExpandPath<T, U> : never

// TODO 'positions.assortment,agent.attributes.value'
// TODO 'attributes.value,agent.attributes.value'
Expand Down
10 changes: 10 additions & 0 deletions test/model/utils/Expand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import {
Meta
} from '../../../src'

//#region Пустой Expand

const t71: 'foo' = {} as Expand<'foo', undefined>
t71

const t70: CustomerOrder = {} as Expand<CustomerOrder, undefined>
t70

//#endregion

//#region Expand не должен затрагивать прочие поля
const t00 = {} as Expand<CustomerOrder, 'agent'>

Expand Down

0 comments on commit dd099ec

Please sign in to comment.