From 0f70fcc5fe361e4215df2a7d72bb6361d6095221 Mon Sep 17 00:00:00 2001 From: "Vitaliy V. Makeev" Date: Fri, 29 Jul 2022 10:25:29 +0500 Subject: [PATCH] =?UTF-8?q?fix(Attribute):=20=D0=B7=D0=BD=D0=B0=D1=87?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B0=D1=82=D1=80=D0=B8=D0=B1=D1=83?= =?UTF-8?q?=D1=82=D0=B0=20=D0=BD=D0=B5=20=D0=B4=D0=BE=D0=BB=D0=B6=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=B1=D1=8B=D1=82=D1=8C=20undefined?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/model/Attribute.ts | 8 ++--- test/model/attributes.test-d.ts | 35 ++++++++++++++++++++++ test/model/utils/Patch.test.ts | 52 ++++++++++++++++++++++++++++++++- 3 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 test/model/attributes.test-d.ts diff --git a/src/model/Attribute.ts b/src/model/Attribute.ts index bfd1013..483c3f6 100644 --- a/src/model/Attribute.ts +++ b/src/model/Attribute.ts @@ -1,4 +1,4 @@ -import type { PartialNullable } from '../tools' +import type { NullablePartial } from '../tools' import type { Entity } from './Entity' import type { EntityPatchRef, EntityRef } from './EntityRef' import type { MediaType } from './MediaType' @@ -92,10 +92,10 @@ export type Attribute = export type AttributePatch = T extends AttributeType.File ? EntityPatchRef<'attributemetadata'> & { - file?: { + file: { filename: string content: string - } + } | null } : T extends AttributeType.CustomEntity @@ -103,4 +103,4 @@ export type AttributePatch = value: EntityRef<'customentity'> } - : EntityPatchRef<'attributemetadata'> & PartialNullable, 'value'>> + : EntityPatchRef<'attributemetadata'> & NullablePartial, 'value'>> diff --git a/test/model/attributes.test-d.ts b/test/model/attributes.test-d.ts new file mode 100644 index 0000000..ed077c7 --- /dev/null +++ b/test/model/attributes.test-d.ts @@ -0,0 +1,35 @@ +import { expectType, expectNotType } from 'tsd' +import { Attribute, AttributePatch, AttributeType } from '../../src' + +const t10_1 = {} as Attribute + +if (t10_1.type === AttributeType.Boolean) { + expectType(t10_1.value) +} + +const t11_1 = {} as AttributePatch + +expectType<'attributemetadata'>(t11_1.meta.type) + +// @ts-expect-error value maybe undefined if file specified +t11_1.value + +if ('value' in t11_1) { + // @ts-expect-error Поле `file` не определено если указано `value` + t11_1.file + + // value не должен быть undefined, если он указан + expectType({} as any as Extract) + + expectType({} as any as Extract) +} + +if ('file' in t11_1) { + // @ts-expect-error Поле `value` не определено если указано `file` + t11_1.value + + // value не должен быть undefined, если он указан + expectType({} as any as Extract) + + expectType({} as any as Extract) +} diff --git a/test/model/utils/Patch.test.ts b/test/model/utils/Patch.test.ts index 153a1a1..6074fcb 100644 --- a/test/model/utils/Patch.test.ts +++ b/test/model/utils/Patch.test.ts @@ -59,6 +59,16 @@ t1.attributes = [ } ] +t1.attributes = [ + // @ts-expect-error Нельзя указывать атрибуты без значений + { + meta: { + type: 'attributemetadata', + href: '' + } + } +] + t1.attributes = [ { meta: { @@ -71,7 +81,22 @@ t1.attributes = [ meta: { type: 'attributemetadata', href: '' - } + }, + file: null + }, + { + meta: { + type: 'attributemetadata', + href: '' + }, + value: 'foo' + }, + { + meta: { + type: 'attributemetadata', + href: '' + }, + value: null } ] @@ -128,7 +153,32 @@ const t4: PatchCollection<'customerorder'> = [ meta: { type: 'attributemetadata', href: '' + }, + value: true + }, + { + meta: { + type: 'attributemetadata', + href: '' + }, + value: null + }, + { + meta: { + type: 'attributemetadata', + href: '' + }, + file: { + filename: 'foo.jpg', + content: '' } + }, + { + meta: { + type: 'attributemetadata', + href: '' + }, + file: null } ], positions: [