gpt4 book ai didi

typescript - 无法搜索只读数组联合

转载 作者:行者123 更新时间:2023-12-05 05:55:45 26 4
gpt4 key购买 nike

我想在只读数组联合中搜索一个元素

const areas = {
area1: {
adjacencies: [2, 3, 4, 5]
},
area2: {
adjacencies: [6, 7, 8]
}
} as const;
let area: keyof typeof areas;
if (Math.random() < 0.5) {
area = "area1";
} else {
area = "area2"
}

// Argument of type 'number' is not assignable to parameter of type 'never'
areas[area].adjacencies.includes(3);

我也试过indexOf ,但它也没有用。我找到了 includes类型是 ReadonlyArray<T>.includes(searchElement: never, fromIndex: number | undefined): boolean .

我想 includes类型应该是两个只读数组元素的并集,如下所示:

const areas = {
area1: {
adjacencies: [2, 3, 4, 5]
},
area2: {
adjacencies: [6, 7, 8]
}
} as const;

type ValueOf<T extends object> = T[keyof T];
type Values = ValueOf<ValueOf<typeof areas>>
type ElementUnion = Values[number];

let area: keyof typeof areas;
if (Math.random() < 0.5) {
area = "area1";
} else {
area = "area2"
}

areas[area].adjacencies.includes(3);

如何申请ElementUnionincludesindexOf ??

这是 playground

最佳答案

当你执行 as const 时,你会告诉 typescript “即使遇到像 0 或“hello”这样的文字,而不是将其推断为一般数字或字符串,而是按字面推断它。

因此,除非两个数组都有您要搜索的元素,否则它不会工作。

const areas = {
area1: {
adjacencies: [2, 3, 4, 5]
},
area2: {
adjacencies: [6, 7, 8, 3]
}
} as const;

type ValueOf<T extends object> = T[keyof T];
type Values = ValueOf<ValueOf<typeof areas>>
type ElementUnion = Values[number];

let area: keyof typeof areas;
if (Math.random() < 0.5) {
area = "area1";
} else {
area = "area2"
}

areas[area].adjacencies.includes(3);

请注意我是如何将 3 添加到第二个数组的。

Playground

编辑:

查看函数的签名,只能将数组的并集传递给它。

(method) ReadonlyArray<T>.includes(searchElement: 3, fromIndex: number | undefined): boolean

编辑 2:
示例:

const areas = {
area1: {
adjacencies: [2, 3, 4, 5]
},
area2: {
adjacencies: [6, 7, 8]
}
} as const;

type ValueOf<T extends object> = T[keyof T];
type Values = ValueOf<ValueOf<typeof areas>>
type ElementUnion = Values[number];

let area: keyof typeof areas;

if (Math.random() < 0.5) {
area = "area1";
} else {
area = "area2"
}

// uh oh, .adjacencies can be either one of the arrays
// if the variable was declared without as const, the function would accept a number
// however since that it not the case, typescript takes the intersection
// (elements present in both arrays) as literal arguments
areas[area].adjacencies.includes(3);

///////////////////////////////////////////////////////////////
if (area === 'area1') {
// here typescript knows what the elements are
// but since the variable was declared as const, only literal values
// that are in the array can be passed to the function
// v Hover over this
areas[area].adjacencies.includes(2);
} else {
// v Hover over this
areas[area].adjacencies.includes(6);
}

// Direct access
// v Hover over this
areas.area1.adjacencies.includes(2);
// v Hover over this
areas.area2.adjacencies.includes(6);
// v Hover over this
areas['area1'].adjacencies.includes(2)
// v Hover over this
areas['area2'].adjacencies.includes(6)

悬停在 Playground 上查看

Playground

关于typescript - 无法搜索只读数组联合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69368524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com