From ff1f554e0beacb5ce2f3308c8212e2b8e4d8cbb0 Mon Sep 17 00:00:00 2001 From: Karl-Philipp Wulfert Date: Wed, 5 Jun 2019 16:12:18 +0200 Subject: [PATCH] feat: add static typed test for consistency Fixes #71 --- package-lock.json | 50 ++++--- package.json | 1 + test/Type.spec.ts | 344 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 369 insertions(+), 26 deletions(-) create mode 100644 test/Type.spec.ts diff --git a/package-lock.json b/package-lock.json index ca9c08cf..f4de8954 100644 --- a/package-lock.json +++ b/package-lock.json @@ -692,6 +692,12 @@ "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", "dev": true }, + "conditional-type-checks": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/conditional-type-checks/-/conditional-type-checks-1.0.1.tgz", + "integrity": "sha512-+O/IE/QGjXzdk4ttJFLRQm8pofQE0KZZmgcamquUGhHfaZt0UTZMlklqW0J9tWO8HQvC70cPahEAVMwSfeqtuQ==", + "dev": true + }, "conventional-changelog": { "version": "3.1.8", "resolved": "https://registry.npmjs.org/conventional-changelog/-/conventional-changelog-3.1.8.tgz", @@ -836,9 +842,9 @@ "dev": true }, "conventional-changelog-writer": { - "version": "4.0.5", - "resolved": "https://registry.npmjs.org/conventional-changelog-writer/-/conventional-changelog-writer-4.0.5.tgz", - "integrity": "sha512-g/Myp4MaJ1A+f7Ai+SnVhkcWtaHk6flw0SYN7A+vQ+MTu0+gSovQWs4Pg4NtcNUcIztYQ9YHsoxHP+GGQplI7Q==", + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/conventional-changelog-writer/-/conventional-changelog-writer-4.0.6.tgz", + "integrity": "sha512-ou/sbrplJMM6KQpR5rKFYNVQYesFjN7WpNGdudQSWNi6X+RgyFUcSv871YBYkrUYV9EX8ijMohYVzn9RUb+4ag==", "dev": true, "requires": { "compare-func": "^1.3.1", @@ -848,17 +854,9 @@ "json-stringify-safe": "^5.0.1", "lodash": "^4.2.1", "meow": "^4.0.0", - "semver": "^5.5.0", + "semver": "^6.0.0", "split": "^1.0.0", "through2": "^3.0.0" - }, - "dependencies": { - "semver": { - "version": "5.7.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.0.tgz", - "integrity": "sha512-Ya52jSX2u7QKghxeoFGpLwCtGlt7j0oY9DYb5apt9nPlJ42ID+ulTXESnt/qAQcoSERyZ5sl3LDIOw0nAn/5DA==", - "dev": true - } } }, "conventional-commits-filter": { @@ -872,13 +870,13 @@ } }, "conventional-commits-parser": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/conventional-commits-parser/-/conventional-commits-parser-3.0.2.tgz", - "integrity": "sha512-y5eqgaKR0F6xsBNVSQ/5cI5qIF3MojddSUi1vKIggRkqUTbkqFKH9P5YX/AT1BVZp9DtSzBTIkvjyVLotLsVog==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/conventional-commits-parser/-/conventional-commits-parser-3.0.3.tgz", + "integrity": "sha512-KaA/2EeUkO4bKjinNfGUyqPTX/6w9JGshuQRik4r/wJz7rUw3+D3fDG6sZSEqJvKILzKXFQuFkpPLclcsAuZcg==", "dev": true, "requires": { "JSONStream": "^1.0.4", - "is-text-path": "^1.0.0", + "is-text-path": "^2.0.0", "lodash": "^4.2.1", "meow": "^4.0.0", "split2": "^2.0.0", @@ -1884,12 +1882,12 @@ } }, "is-text-path": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/is-text-path/-/is-text-path-1.0.1.tgz", - "integrity": "sha1-Thqg+1G/vLPpJogAE5cgLBd1tm4=", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-text-path/-/is-text-path-2.0.0.tgz", + "integrity": "sha512-+oDTluR6WEjdXEJMnC2z6A4FRwFoYuvShVVEGsS7ewc0UTi2QtAKMDJuL4BDEVt+5T7MjFo12RP8ghOM75oKJw==", "dev": true, "requires": { - "text-extensions": "^1.0.0" + "text-extensions": "^2.0.0" } }, "is-utf8": { @@ -3490,9 +3488,9 @@ } }, "text-extensions": { - "version": "1.9.0", - "resolved": "https://registry.npmjs.org/text-extensions/-/text-extensions-1.9.0.tgz", - "integrity": "sha512-wiBrwC1EhBelW12Zy26JeOUkQ5mRu+5o8rpsJk5+2t+Y5vE7e842qtZDQ2g1NpX/29HdyFeJ4nSIhI47ENSxlQ==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/text-extensions/-/text-extensions-2.0.0.tgz", + "integrity": "sha512-F91ZqLgvi1E0PdvmxMgp+gcf6q8fMH7mhdwWfzXnl1k+GbpQDmi8l7DzLC5JTASKbwpY3TfxajAUzAXcv2NmsQ==", "dev": true }, "through": { @@ -3702,9 +3700,9 @@ "dev": true }, "uglify-js": { - "version": "3.5.12", - "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.5.12.tgz", - "integrity": "sha512-KeQesOpPiZNgVwJj8Ge3P4JYbQHUdZzpx6Fahy6eKAYRSV4zhVmLXoC+JtOeYxcHCHTve8RG1ZGdTvpeOUM26Q==", + "version": "3.5.14", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.5.14.tgz", + "integrity": "sha512-dgyjIw8KFK6AyVl5vm2tEqPewv5TKGEiiVFLI1LbF+oHua/Njd8tZk3lIbF1AWU1rNdEg7scaceADb4zqCcWXg==", "dev": true, "optional": true, "requires": { diff --git a/package.json b/package.json index c27d5110..74be808c 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "@types/rimraf": "2.0.2", "chai": "4.2.0", "commander": "2.20.0", + "conditional-type-checks": "1.0.1", "conventional-changelog-cli": "2.0.21", "mocha": "6.1.4", "mocha-typescript": "1.1.17", diff --git a/test/Type.spec.ts b/test/Type.spec.ts new file mode 100644 index 00000000..972ab96f --- /dev/null +++ b/test/Type.spec.ts @@ -0,0 +1,344 @@ +import {assert, Has, IsAny, IsNever, NotHas} from 'conditional-type-checks'; +import {SCThing, SCThingWithoutReferences} from '../src/core/Thing'; +import {SCAcademicEvent, SCAcademicEventWithoutReferences} from '../src/core/things/AcademicEvent'; +import {SCArticle, SCArticleWithoutReferences} from '../src/core/things/Article'; +import {SCBook, SCBookWithoutReferences} from '../src/core/things/Book'; +import {SCBuilding, SCBuildingWithoutReferences} from '../src/core/things/Building'; +import {SCCatalog, SCCatalogWithoutReferences} from '../src/core/things/Catalog'; +import {SCCourseOfStudies, SCCourseOfStudiesWithoutReferences} from '../src/core/things/CourseOfStudies'; +import {SCDateSeries, SCDateSeriesWithoutReferences} from '../src/core/things/DateSeries'; +import {SCDiff, SCDiffWithoutReferences} from '../src/core/things/Diff'; +import {SCDish, SCDishWithoutReferences} from '../src/core/things/Dish'; +import {SCFavorite, SCFavoriteWithoutReferences} from '../src/core/things/Favorite'; +import {SCFloor, SCFloorWithoutReferences} from '../src/core/things/Floor'; +import {SCMessage, SCMessageWithoutReferences} from '../src/core/things/Message'; +import {SCOrganization, SCOrganizationWithoutReferences} from '../src/core/things/Organization'; +import {SCPerson, SCPersonWithoutReferences} from '../src/core/things/Person'; +import {SCPointOfInterest, SCPointOfInterestWithoutReferences} from '../src/core/things/PointOfInterest'; +import {SCRoom, SCRoomWithoutReferences} from '../src/core/things/Room'; +import {SCSemester, SCSemesterWithoutReferences} from '../src/core/things/Semester'; +import {SCSetting, SCSettingWithoutReferences} from '../src/core/things/Setting'; +import {SCSportCourse, SCSportCourseWithoutReferences} from '../src/core/things/SportCourse'; +import {SCStudyModule, SCStudyModuleWithoutReferences} from '../src/core/things/StudyModule'; +import {SCTicket, SCTicketWithoutReferences} from '../src/core/things/Ticket'; +import {SCToDo, SCToDoWithoutReferences} from '../src/core/things/ToDo'; +import {SCTour, SCTourWithoutReferences} from '../src/core/things/Tour'; +import {SCVideo, SCVideoWithoutReferences} from '../src/core/things/Video'; + +/** + * Check if E extends T + */ +type Extends = E extends T ? true : false; + +/** + * Get type of array elements up to nesting level 3 + */ +type ElementType = T extends any[] ? + (T[0] extends any[] ? + (T[0][0] extends any[] ? + T[0][0][0] : T[0][0]) : T[0]) : T; + +/** + * Get types of properties + * + * - Extracts only the properties which extend object and are not any. + * - If type is an array it returns the type of the elements. + */ +type PropertyTypes = Extract extends true ? + never : T[keyof T]) : never> + , object>; + +/** + * Get nested property types + */ +type PropertyTypesNested = PropertyTypes extends object ? PropertyTypes> : PropertyTypes; + +/** + * Types of properties of SCDiff + */ +type SCDiffPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCAcademicEvent + */ +type SCAcademicEventPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCArticle + */ +type SCArticlePropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCBook + */ +type SCBookPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCBuilding + */ +type SCBuildingPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCCatalog + */ +type SCCatalogPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCCourseOfStudies + */ +type SCCourseOfStudiesPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCDateSeries + */ +type SCDateSeriesPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCDish + */ +type SCDishPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCFavorite + */ +type SCFavoritePropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCFloor + */ +type SCFloorPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCMessage + */ +type SCMessagePropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCOrganization + */ +type SCOrganizationPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCPerson + */ +type SCPersonPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCPointOfInterest + */ +type SCPointOfInterestPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCRoom + */ +type SCRoomPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCSemester + */ +type SCSemesterPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCSetting + */ +type SCSettingPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCSportCourse + */ +type SCSportCoursePropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCStudyModule + */ +type SCStudyModulePropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCTicket + */ +type SCTicketPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCToDo + */ +type SCToDoPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCToDo + */ +type SCTourPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Types of properties of SCVideo + */ +type SCVideoPropertyTypes = PropertyTypesNested; +assert>(false); +assert>(true); +assert>(true); +assert>(false); +assert>(false); +assert>(true); + +/** + * Dummy interface, to check if union types still resolve to any if one of the members is any + */ +interface Foo { + bar: SCPerson; + foo: any; +} + +/** + * Type that is unfortunately never - blame TypeScript + */ +type UnfortunatelyNever = PropertyTypesNested; +assert>(true); + +/** + * Flat property types + */ +type FlatPropertyTypes = T[keyof T]; + +/** + * Type that is unfortunately any - blame TypeScript + */ +type UnfortunatelyAny = FlatPropertyTypes; +assert>(true);