首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >品牌类型交集

品牌类型交集
EN

Stack Overflow用户
提问于 2021-06-20 15:13:03
回答 1查看 206关注 0票数 1

typescript 3.7.5下,我有以下类型:

代码语言:javascript
复制
interface Branding<BrandT> { readonly __brand: BrandT; }
type Brand<T, BrandT> = T & Branding<BrandT>;

interface SortableRegion { start: number; end: number; }
type SortedRegionArray<T extends SortableRegion> = Brand<T[], 'sorted'>;
type SortedUniqueRegionArray<T extends SortableRegion> = Brand<SortedRegionArray<T>, 'unique'>;

计算的SortedUniqueRegionArray类型为:

type SortedUniqueRegionArray<T extends SortableRegion> = T[] & Branding<"sorted"> & Branding<"unique">

因此,我有了排序和唯一数组的功能,可以分配给一个排序的数组,而不是相反的。

typescript 4.3.2下,类型SortedUniqueRegionArray的计算结果为:

type SortedUniqueRegionArray<T extends SortableRegion> = never

因此,许多代码无法编译。

怎样才能在打字稿中正确地插入品牌类型?

游乐场

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-05 16:07:10

我不认为你能用一个品牌价值来做这件事。

因为这样的类型总是会导致类型never

代码语言:javascript
复制
let value: "unique" & "sorted"; // type of value = never

但是,您可以引入多个品牌价值。

在您的情况下,一个用于唯一性,另一个用于排序。

注意:我从来没有使用过/需要过这样的东西,所以我不知道它在长期内会有多好,或者考虑它是否是一个好主意。

扩展您的示例:

代码语言:javascript
复制
type MultiBranding<Type> = {
    [Property in keyof Type as `__brand_${string & Property}`]: Type[Property]
};
type MultiBrand<T, BrandT> = T & MultiBranding<BrandT>;

// Examples
export interface SortableRegion { start: number; end: number; }

type Unique<T extends SortableRegion> = MultiBrand<T[], { uniqueness: "unique" }>
type Sorted<T extends SortableRegion> = MultiBrand<T[], { sorting: "sorted" }>

let sortedArray: Sorted<SortableRegion> = [] as unknown as Sorted<SortableRegion>;
let uniqueArray: Unique<SortableRegion> = [] as unknown as Unique<SortableRegion>;

// This fancyArray represents the goal you're trying to achieve
let fancyArray: Sorted<SortableRegion> & Unique<SortableRegion> = [] as unknown as Sorted<SortableRegion> & Unique<SortableRegion>;

sortedArray = unsortedArray; // fails
sortedArray = uniqueArray; // fails
fancyArray = sortedArray; // fails
fancyArray = uniqueArray; // fails

sortedArray = fancyArray; // succeeds
uniqueArray = fancyArray; // succeeds

// For completeness
type Unsorted<T extends SortableRegion> = MultiBrand<T[], { sorting: "unsorted" }>
type NotUnique<T extends SortableRegion> = MultiBrand<T[], { uniqueness: "not-unique" }>

let unsortedArray: Unsorted<SortableRegion> = [] as unknown as Unsorted<SortableRegion>;
let notUniqueArray: NotUnique<SortableRegion> = [] as unknown as NotUnique<SortableRegion>;

unsortedArray = fancyArray; // fails
notUniqueArray = fancyArray; // fails

游乐场

注意:如果您只有有限数量的唯一/排序组合,那么我将考虑创建一个代表每个案例的类型,并查看它是如何发展的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68057233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档