From fa17c73c45be3f894ed958d658f749a3cabff0ee Mon Sep 17 00:00:00 2001 From: Sigurd Fosseng Date: Thu, 26 Nov 2020 10:57:51 +0100 Subject: [PATCH] Test type intersection with reuse --- api.md | 20 +++++++ test/programs/type-intersection-reuse/main.ts | 14 +++++ .../type-intersection-reuse/schema.json | 53 +++++++++++++++++++ test/schema.test.ts | 3 ++ 4 files changed, 90 insertions(+) create mode 100644 test/programs/type-intersection-reuse/main.ts create mode 100644 test/programs/type-intersection-reuse/schema.json diff --git a/api.md b/api.md index 7c8d3de1..dd063487 100644 --- a/api.md +++ b/api.md @@ -1550,6 +1550,26 @@ interface Foo { ``` +## [type-intersection-reuse](./test/programs/type-intersection-reuse) + +```ts +interface Type1 { + value1: string; + value2: number; +} +interface Type2 { + value2: number; + value3: boolean; +} + +// Type1 can be reused, make sure value2 still works. +interface MyObject { + value1: Type1; + value2: Type1 & Type2; +} +``` + + ## [type-mapped-types](./test/programs/type-mapped-types) ```ts diff --git a/test/programs/type-intersection-reuse/main.ts b/test/programs/type-intersection-reuse/main.ts new file mode 100644 index 00000000..c96739dc --- /dev/null +++ b/test/programs/type-intersection-reuse/main.ts @@ -0,0 +1,14 @@ +interface Type1 { + value1: string; + value2: number; +} +interface Type2 { + value2: number; + value3: boolean; +} + +// Type1 can be reused, make sure value2 still works. +interface MyObject { + value1: Type1; + value2: Type1 & Type2; +} diff --git a/test/programs/type-intersection-reuse/schema.json b/test/programs/type-intersection-reuse/schema.json new file mode 100644 index 00000000..31cf0a00 --- /dev/null +++ b/test/programs/type-intersection-reuse/schema.json @@ -0,0 +1,53 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "additionalProperties": false, + "definitions": { + "Type1": { + "additionalProperties": false, + "properties": { + "value1": { + "type": "string" + }, + "value2": { + "type": "number" + } + }, + "required": [ + "value1", + "value2" + ], + "type": "object" + } + }, + "properties": { + "value1": { + "$ref": "#/definitions/Type1" + }, + "value2": { + "additionalProperties": false, + "properties": { + "value1": { + "type": "string" + }, + "value2": { + "type": "number" + }, + "value3": { + "type": "boolean" + } + }, + "required": [ + "value1", + "value2", + "value3" + ], + "type": "object" + } + }, + "required": [ + "value1", + "value2" + ], + "type": "object" +} + diff --git a/test/schema.test.ts b/test/schema.test.ts index cb8dd840..524fbc2d 100644 --- a/test/schema.test.ts +++ b/test/schema.test.ts @@ -226,6 +226,9 @@ describe("schema", () => { assertSchema("type-intersection", "MyObject", { noExtraProps: true, }); + assertSchema("type-intersection-reuse", "MyObject", { + noExtraProps: true, + }); assertSchema("type-union-tagged", "Shape"); assertSchema("type-aliases-union-namespace", "MyModel"); assertSchema("type-intersection-recursive", "*");