feat: add static typed test for consistency

Fixes #71
This commit is contained in:
Karl-Philipp Wulfert
2019-06-05 16:12:18 +02:00
parent 9c424b0f96
commit ff1f554e0b
3 changed files with 369 additions and 26 deletions

344
test/Type.spec.ts Normal file
View File

@@ -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, T> = E extends T ? true : false;
/**
* Get type of array elements up to nesting level 3
*/
type ElementType<T> = 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<T> = Extract<ElementType<T extends object ?
(IsAny<T[keyof T]> extends true ?
never : T[keyof T]) : never>
, object>;
/**
* Get nested property types
*/
type PropertyTypesNested<T> = PropertyTypes<T> extends object ? PropertyTypes<PropertyTypes<T>> : PropertyTypes<T>;
/**
* Types of properties of SCDiff
*/
type SCDiffPropertyTypes = PropertyTypesNested<SCDiff>;
assert<NotHas<SCDiffPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCDiffPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCDiffPropertyTypes, SCThing>>(true);
assert<Has<SCDiffPropertyTypes, SCThing>>(false);
assert<Extends<SCDiffWithoutReferences, SCThing>>(false);
assert<Extends<SCDiff, SCThing>>(true);
/**
* Types of properties of SCAcademicEvent
*/
type SCAcademicEventPropertyTypes = PropertyTypesNested<SCAcademicEvent>;
assert<NotHas<SCAcademicEventPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCAcademicEventPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCAcademicEventPropertyTypes, SCThing>>(true);
assert<Has<SCAcademicEventPropertyTypes, SCThing>>(false);
assert<Extends<SCAcademicEventWithoutReferences, SCThing>>(false);
assert<Extends<SCAcademicEvent, SCThing>>(true);
/**
* Types of properties of SCArticle
*/
type SCArticlePropertyTypes = PropertyTypesNested<SCArticle>;
assert<NotHas<SCArticlePropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCArticlePropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCArticlePropertyTypes, SCThing>>(true);
assert<Has<SCArticlePropertyTypes, SCThing>>(false);
assert<Extends<SCArticleWithoutReferences, SCThing>>(false);
assert<Extends<SCArticle, SCThing>>(true);
/**
* Types of properties of SCBook
*/
type SCBookPropertyTypes = PropertyTypesNested<SCBook>;
assert<NotHas<SCBookPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCBookPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCBookPropertyTypes, SCThing>>(true);
assert<Has<SCBookPropertyTypes, SCThing>>(false);
assert<Extends<SCBookWithoutReferences, SCThing>>(false);
assert<Extends<SCBook, SCThing>>(true);
/**
* Types of properties of SCBuilding
*/
type SCBuildingPropertyTypes = PropertyTypesNested<SCBuilding>;
assert<NotHas<SCBuildingPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCBuildingPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCBuildingPropertyTypes, SCThing>>(true);
assert<Has<SCBuildingPropertyTypes, SCThing>>(false);
assert<Extends<SCBuildingWithoutReferences, SCThing>>(false);
assert<Extends<SCBuilding, SCThing>>(true);
/**
* Types of properties of SCCatalog
*/
type SCCatalogPropertyTypes = PropertyTypesNested<SCCatalog>;
assert<NotHas<SCCatalogPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCCatalogPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCCatalogPropertyTypes, SCThing>>(true);
assert<Has<SCCatalogPropertyTypes, SCThing>>(false);
assert<Extends<SCCatalogWithoutReferences, SCThing>>(false);
assert<Extends<SCCatalog, SCThing>>(true);
/**
* Types of properties of SCCourseOfStudies
*/
type SCCourseOfStudiesPropertyTypes = PropertyTypesNested<SCCourseOfStudies>;
assert<NotHas<SCCourseOfStudiesPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCCourseOfStudiesPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCCourseOfStudiesPropertyTypes, SCThing>>(true);
assert<Has<SCCourseOfStudiesPropertyTypes, SCThing>>(false);
assert<Extends<SCCourseOfStudiesWithoutReferences, SCThing>>(false);
assert<Extends<SCCourseOfStudies, SCThing>>(true);
/**
* Types of properties of SCDateSeries
*/
type SCDateSeriesPropertyTypes = PropertyTypesNested<SCDateSeries>;
assert<NotHas<SCDateSeriesPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCDateSeriesPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCDateSeriesPropertyTypes, SCThing>>(true);
assert<Has<SCDateSeriesPropertyTypes, SCThing>>(false);
assert<Extends<SCDateSeriesWithoutReferences, SCThing>>(false);
assert<Extends<SCDateSeries, SCThing>>(true);
/**
* Types of properties of SCDish
*/
type SCDishPropertyTypes = PropertyTypesNested<SCDish>;
assert<NotHas<SCDishPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCDishPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCDishPropertyTypes, SCThing>>(true);
assert<Has<SCDishPropertyTypes, SCThing>>(false);
assert<Extends<SCDishWithoutReferences, SCThing>>(false);
assert<Extends<SCDish, SCThing>>(true);
/**
* Types of properties of SCFavorite
*/
type SCFavoritePropertyTypes = PropertyTypesNested<SCFavorite>;
assert<NotHas<SCFavoritePropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCFavoritePropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCFavoritePropertyTypes, SCThing>>(true);
assert<Has<SCFavoritePropertyTypes, SCThing>>(false);
assert<Extends<SCFavoriteWithoutReferences, SCThing>>(false);
assert<Extends<SCFavorite, SCThing>>(true);
/**
* Types of properties of SCFloor
*/
type SCFloorPropertyTypes = PropertyTypesNested<SCFloor>;
assert<NotHas<SCFloorPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCFloorPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCFloorPropertyTypes, SCThing>>(true);
assert<Has<SCFloorPropertyTypes, SCThing>>(false);
assert<Extends<SCFloorWithoutReferences, SCThing>>(false);
assert<Extends<SCFloor, SCThing>>(true);
/**
* Types of properties of SCMessage
*/
type SCMessagePropertyTypes = PropertyTypesNested<SCMessage>;
assert<NotHas<SCMessagePropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCMessagePropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCMessagePropertyTypes, SCThing>>(true);
assert<Has<SCMessagePropertyTypes, SCThing>>(false);
assert<Extends<SCMessageWithoutReferences, SCThing>>(false);
assert<Extends<SCMessage, SCThing>>(true);
/**
* Types of properties of SCOrganization
*/
type SCOrganizationPropertyTypes = PropertyTypesNested<SCOrganization>;
assert<NotHas<SCOrganizationPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCOrganizationPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCOrganizationPropertyTypes, SCThing>>(true);
assert<Has<SCOrganizationPropertyTypes, SCThing>>(false);
assert<Extends<SCOrganizationWithoutReferences, SCThing>>(false);
assert<Extends<SCOrganization, SCThing>>(true);
/**
* Types of properties of SCPerson
*/
type SCPersonPropertyTypes = PropertyTypesNested<SCPerson>;
assert<NotHas<SCPersonPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCPersonPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCPersonPropertyTypes, SCThing>>(true);
assert<Has<SCPersonPropertyTypes, SCThing>>(false);
assert<Extends<SCPersonWithoutReferences, SCThing>>(false);
assert<Extends<SCPerson, SCThing>>(true);
/**
* Types of properties of SCPointOfInterest
*/
type SCPointOfInterestPropertyTypes = PropertyTypesNested<SCPointOfInterest>;
assert<NotHas<SCPointOfInterestPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCPointOfInterestPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCPointOfInterestPropertyTypes, SCThing>>(true);
assert<Has<SCPointOfInterestPropertyTypes, SCThing>>(false);
assert<Extends<SCPointOfInterestWithoutReferences, SCThing>>(false);
assert<Extends<SCPointOfInterest, SCThing>>(true);
/**
* Types of properties of SCRoom
*/
type SCRoomPropertyTypes = PropertyTypesNested<SCRoom>;
assert<NotHas<SCRoomPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCRoomPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCRoomPropertyTypes, SCThing>>(true);
assert<Has<SCRoomPropertyTypes, SCThing>>(false);
assert<Extends<SCRoomWithoutReferences, SCThing>>(false);
assert<Extends<SCRoom, SCThing>>(true);
/**
* Types of properties of SCSemester
*/
type SCSemesterPropertyTypes = PropertyTypesNested<SCSemester>;
assert<NotHas<SCSemesterPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCSemesterPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCSemesterPropertyTypes, SCThing>>(true);
assert<Has<SCSemesterPropertyTypes, SCThing>>(false);
assert<Extends<SCSemesterWithoutReferences, SCThing>>(false);
assert<Extends<SCSemester, SCThing>>(true);
/**
* Types of properties of SCSetting
*/
type SCSettingPropertyTypes = PropertyTypesNested<SCSetting>;
assert<NotHas<SCSettingPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCSettingPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCSettingPropertyTypes, SCThing>>(true);
assert<Has<SCSettingPropertyTypes, SCThing>>(false);
assert<Extends<SCSettingWithoutReferences, SCThing>>(false);
assert<Extends<SCSetting, SCThing>>(true);
/**
* Types of properties of SCSportCourse
*/
type SCSportCoursePropertyTypes = PropertyTypesNested<SCSportCourse>;
assert<NotHas<SCSportCoursePropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCSportCoursePropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCSportCoursePropertyTypes, SCThing>>(true);
assert<Has<SCSportCoursePropertyTypes, SCThing>>(false);
assert<Extends<SCSportCourseWithoutReferences, SCThing>>(false);
assert<Extends<SCSportCourse, SCThing>>(true);
/**
* Types of properties of SCStudyModule
*/
type SCStudyModulePropertyTypes = PropertyTypesNested<SCStudyModule>;
assert<NotHas<SCStudyModulePropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCStudyModulePropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCStudyModulePropertyTypes, SCThing>>(true);
assert<Has<SCStudyModulePropertyTypes, SCThing>>(false);
assert<Extends<SCStudyModuleWithoutReferences, SCThing>>(false);
assert<Extends<SCStudyModule, SCThing>>(true);
/**
* Types of properties of SCTicket
*/
type SCTicketPropertyTypes = PropertyTypesNested<SCTicket>;
assert<NotHas<SCTicketPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCTicketPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCTicketPropertyTypes, SCThing>>(true);
assert<Has<SCTicketPropertyTypes, SCThing>>(false);
assert<Extends<SCTicketWithoutReferences, SCThing>>(false);
assert<Extends<SCTicket, SCThing>>(true);
/**
* Types of properties of SCToDo
*/
type SCToDoPropertyTypes = PropertyTypesNested<SCToDo>;
assert<NotHas<SCToDoPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCToDoPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCToDoPropertyTypes, SCThing>>(true);
assert<Has<SCToDoPropertyTypes, SCThing>>(false);
assert<Extends<SCToDoWithoutReferences, SCThing>>(false);
assert<Extends<SCToDo, SCThing>>(true);
/**
* Types of properties of SCToDo
*/
type SCTourPropertyTypes = PropertyTypesNested<SCTour>;
assert<NotHas<SCTourPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCTourPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCTourPropertyTypes, SCThing>>(true);
assert<Has<SCTourPropertyTypes, SCThing>>(false);
assert<Extends<SCTourWithoutReferences, SCThing>>(false);
assert<Extends<SCTour, SCThing>>(true);
/**
* Types of properties of SCVideo
*/
type SCVideoPropertyTypes = PropertyTypesNested<SCVideo>;
assert<NotHas<SCVideoPropertyTypes, SCThingWithoutReferences>>(false);
assert<Has<SCVideoPropertyTypes, SCThingWithoutReferences>>(true);
assert<NotHas<SCVideoPropertyTypes, SCThing>>(true);
assert<Has<SCVideoPropertyTypes, SCThing>>(false);
assert<Extends<SCVideoWithoutReferences, SCThing>>(false);
assert<Extends<SCVideo, SCThing>>(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<Foo>;
assert<IsNever<UnfortunatelyNever>>(true);
/**
* Flat property types
*/
type FlatPropertyTypes<T> = T[keyof T];
/**
* Type that is unfortunately any - blame TypeScript
*/
type UnfortunatelyAny = FlatPropertyTypes<Foo>;
assert<IsAny<UnfortunatelyAny>>(true);