在typescript 3.7.5下,我有以下类型:
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
因此,许多代码无法编译。
怎样才能在打字稿中正确地插入品牌类型?
发布于 2021-08-05 16:07:10
我不认为你能用一个品牌价值来做这件事。
因为这样的类型总是会导致类型never
let value: "unique" & "sorted"; // type of value = never但是,您可以引入多个品牌价值。
在您的情况下,一个用于唯一性,另一个用于排序。
注意:我从来没有使用过/需要过这样的东西,所以我不知道它在长期内会有多好,或者考虑它是否是一个好主意。
扩展您的示例:
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注意:如果您只有有限数量的唯一/排序组合,那么我将考虑创建一个代表每个案例的类型,并查看它是如何发展的。
https://stackoverflow.com/questions/68057233
复制相似问题