英文:
Proper way to handle types for JS reduce with a list having union of types?
问题
以下是您要翻译的内容:
"Let's say we have a function segregateIds that separates a list of Ids into two parts by checking in a string-to-boolean map.
const segregateIds = (
ids: string[] | number[],
optionsById: Record<string, boolean>,
): { correctIds: string[] | number[]; incorrectIds: string[] | number[] } => {
return ids.reduce<{ correctIds: string[] | number[]; incorrectIds: string[] | number[] }>(
(accum: { correctIds: string[] | number[]; incorrectIds: string[] | number[] }, id: string | number) => {
if (optionsById[id]) {
accum.correctIds.push(id);
} else {
accum.incorrectIds.push(id);
}
return accum;
},
{
correctIds: [],
incorrectIds: [],
}
);
};
The problem here is that reduce is not accepting a list that has both string[] or number[]. Type coercion is a way to handle this. Is there something better?
Here is the Link to TS Playground
Expecting to resolve the TS error. Tried to type-coerce the accumulator, id but in vain. Tried using lodash reduce, partition but still error remains.
I can use Array<string|number>
and pass to lodash partition but that will be tweaking the actual type as Array<string|number>
is different from string[] | number[]
."
英文:
Let's say we have a function segregateIds that separates a list of Ids into two parts by checking in a string-to-boolean map.
const segregateIds = (
ids: string[] | number[],
optionsById: Record<string, boolean>,
): { correctIds: string[] | number[]; incorrectIds: string[] | number[] } => {
return ids.reduce<{ correctIds: string[] | number[]; incorrectIds: string[] | number[] }>(
(accum: { correctIds: string[] | number[]; incorrectIds: string[] | number[] }, id: string | number) => {
if (optionsById[id]) {
accum.correctIds.push(id);
} else {
accum.incorrectIds.push(id);
}
return accum;
},
{
correctIds: [],
incorrectIds: [],
}
);
};
The problem here is that reduce is not accepting a list that has both string[] or number[]. Type coercion is a way to handle this. Is there something better?
Here is the Link to TS Playground
Expecting to resolve the TS error. Tried to type-coerce the accumulator, id but in vain. Tried using lodash reduce, partition but still error remains.
I can use Array<string|number>
and pass to lodash partition but that will be tweaking the actual type as Array<string|number>
is different from string[] | number[]
.
答案1
得分: 3
你可以使用通用类型 T
来接受字符串或数字的数组:
const segregateIds = <T extends string | number>(
ids: T[],
optionsById: Record<string, boolean>,
): { correctIds: T[]; incorrectIds: T[] } => {
return ids.reduce<{ correctIds: T[]; incorrectIds: T[] }>(
(accum: { correctIds: T[]; incorrectIds: T[] }, id: T) => {
if (optionsById[id.toString()]) {
accum.correctIds.push(id);
} else {
accum.incorrectIds.push(id);
}
return accum;
},
{
correctIds: [],
incorrectIds: [],
}
);
};
英文:
You can use generic type T
to accept arrays of strings or numbers:
const segregateIds = <T extends string | number>(
ids: T[],
optionsById: Record<string, boolean>,
): { correctIds: T[]; incorrectIds: T[] } => {
return ids.reduce<{ correctIds: T[]; incorrectIds: T[] }>(
(accum: { correctIds: T[]; incorrectIds: T[] }, id: T) => {
if (optionsById[id.toString()]) {
accum.correctIds.push(id);
} else {
accum.incorrectIds.push(id);
}
return accum;
},
{
correctIds: [],
incorrectIds: [],
}
);
答案2
得分: 3
除了 protob 的回答 之外。
由于您将问题标记为 lodash 标签,您可以使用 _.partition()
而不是 reduce:
const segregateIds = <T extends string | number>(
ids: T[],
optionsById: Record<string, boolean>,
): { correctIds: T[]; incorrectIds: T[] } => {
const [correctIds, incorrectIds] = partition(ids, id => optionsById[String(id)]);
return {
correctIds,
incorrectIds
};
}
英文:
In addition to protob's answer.
Since you marked the question with the lodash tag, you can use _.partition()
instead of reduce:
const segregateIds = <T extends string | number>(
ids: T[],
optionsById: Record<string, boolean>,
): { correctIds: T[]; incorrectIds: T[] } => {
const [correctIds, incorrectIds] = partition(ids, id => optionsById[String(id)]);
return {
correctIds,
incorrectIds
};
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论