Skip to content

Commit

Permalink
feat: Adds narrowing types for Taxonomy & Multiple choice elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Enngage committed Sep 10, 2024
1 parent ac3b5a2 commit 3118752
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
10 changes: 5 additions & 5 deletions lib/elements/element-models.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IContentItemSystemAttributes } from '../models/item-models';
import { Contracts } from '../contracts';
import { IContentItemSystemAttributes } from '../models/item-models';
import { ElementType } from './element-type';

export namespace ElementModels {
Expand Down Expand Up @@ -83,13 +83,13 @@ export namespace ElementModels {
url: string;
}

export interface MultipleChoiceOption {
export interface MultipleChoiceOption<TOptionCodename extends string = string> {
name: string;
codename: string;
codename: TOptionCodename;
}

export interface TaxonomyTerm<TaxonomyCodename extends string = string> {
export interface TaxonomyTerm<TTaxonomyCodename extends string = string> {
name: string;
codename: TaxonomyCodename;
codename: TTaxonomyCodename;
}
}
13 changes: 8 additions & 5 deletions lib/elements/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export namespace Elements {
linkedItems: TContentItem[];
};

export type MultipleChoiceElement = ElementModels.IElement<ElementModels.MultipleChoiceOption[]>;
export type MultipleChoiceElement<TOptionCodenames extends string = string> = ElementModels.IElement<
ElementModels.MultipleChoiceOption<TOptionCodenames>[]
>;

export type DateTimeElement = ElementModels.IElement<string | null> & {
/**
Expand Down Expand Up @@ -52,13 +54,14 @@ export namespace Elements {

export type UrlSlugElement = ElementModels.IElement<string>;

export type TaxonomyElement<TaxonomyCodename extends string = string> = ElementModels.IElement<
ElementModels.TaxonomyTerm<TaxonomyCodename>[]
> & {
export type TaxonomyElement<
TaxonomyCodenames extends string = string,
TaxonomyGroupCodename extends string = string
> = ElementModels.IElement<ElementModels.TaxonomyTerm<TaxonomyCodenames>[]> & {
/**
* Taxonomy group
*/
taxonomyGroup: string | null;
taxonomyGroup: TaxonomyGroupCodename;
};

export type UnknownElement = ElementModels.IElement<any>;
Expand Down
7 changes: 4 additions & 3 deletions test/browser/setup/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export type Actor = IContentItem<
WorkflowStepCodenames
>;

export type releasecategoryTaxonomy = 'global_release' | 'us_only' | 'local_release';
export type ReleaseCategoryTaxonomy = 'global_release' | 'us_only' | 'local_release';
export type CategoryMultipleChoiceOptionCodenames = 'sci_fi' | 'action' | 'comedy' | 'drama' | 'romance' | 'animation';

export type Movie = IContentItem<
{
Expand All @@ -27,10 +28,10 @@ export type Movie = IContentItem<
readonly released: Elements.DateTimeElement;
readonly length: Elements.NumberElement;
readonly poster: Elements.AssetsElement;
readonly category: Elements.MultipleChoiceElement;
readonly category: Elements.MultipleChoiceElement<CategoryMultipleChoiceOptionCodenames>;
readonly stars: Elements.LinkedItemsElement<Actor>;
readonly seoname: Elements.UrlSlugElement;
readonly releasecategory: Elements.TaxonomyElement<releasecategoryTaxonomy>;
readonly releasecategory: Elements.TaxonomyElement<ReleaseCategoryTaxonomy, 'releasecategory'>;
},
'movie',
LanguageCodenames,
Expand Down

0 comments on commit 3118752

Please sign in to comment.