有一个与此类似的对象:
const obj = {
1: "one",
2: "two",
3: "three",
}
type Keys = keyof typeof obj; // type of Key is 1 | 2 | 3
Run Code Online (Sandbox Code Playgroud)
我如何获得Keys类型(字符串)"1" | "2" | "3"才能自动完成?
我有一个数组:
const arr = [
{ name: "Jeff", id: 1 },
{ name: "Mary", id: 2 },
{ name: "Jeff", id: 3 },
{ name: "Mary", id: 4 },
{ name: "Emile", id: 5 },
];
Run Code Online (Sandbox Code Playgroud)
我想把它变成这样:
const obj = {
Jeff: [1, 3],
Mary: [2, 4],
Emile: [5],
};
Run Code Online (Sandbox Code Playgroud)
我尝试使用该Array.reduce方法,但我似乎无法理解其中的逻辑。
const obj = arr.reduce((acc, info) => {
acc[info[name]] = ....
return acc;
}, {});
Run Code Online (Sandbox Code Playgroud)
我认为可以用reduce方法来完成
我对打字稿相当陌生,我正在使用并围绕钩子@tanstack/react-table构建一个,它需要 2 个道具并将它们传递给钩子。TableComponentuseReactTabledatacolumnsuseReactTable
问题是 data 和 columns 属性是可以包含不同类型对象的数组,并且它们是连接的,我如何正确输入它们?现在我在里面遇到错误TableComponent
type OrderTableProps = {
data: OrdersRow[];
columns: typeof ordersTableColumns;
onRowClick?: (row: Row<OrdersRow>) => void;
}
type ContainerTableProps = {
data: ContainersRow[];
columns: typeof containersTableColumns;
onRowClick?: (row: Row<ContainersRow>) => void;
}
export type TableComponentProps = PropsWithChildren
& TableComponentBaseProps
& (OrderTableProps | ContainerTableProps);
// TableComponent
const ReactTableComponent = ({data, columns}: TableComponentProps) => {
const table = useReactTable({
data, -> Type 'OrdersRow[] | ContainersRow[]' is not assignable …Run Code Online (Sandbox Code Playgroud)