Syarx
Syarx
TTCTheo's Typesafe Cult
Created by Syarx on 2/8/2024 in #questions
Help me with a typescript generic
Can someone help me with this? I have these types
export type InnerEventObject = {
id: string | number;
a: number;
};

export type EventObject<T> = InnerEventObject & T;

type CombinedData<T> = Readonly<
Record<string, Readonly<{ data: EventObject<T>[] }>>
>;

export function defineConfig<D extends CombinedData<InnerEventObject>>(
props: Record<
keyof D,
{
cell: (event: TODO) => void;
}
>
) {
return props;
}
export type InnerEventObject = {
id: string | number;
a: number;
};

export type EventObject<T> = InnerEventObject & T;

type CombinedData<T> = Readonly<
Record<string, Readonly<{ data: EventObject<T>[] }>>
>;

export function defineConfig<D extends CombinedData<InnerEventObject>>(
props: Record<
keyof D,
{
cell: (event: TODO) => void;
}
>
) {
return props;
}
And this Implementation
import { defineConfig, type EventObject } from "../types";
type Data1 = {
b: string;
};
type Data2 = {
c: string;
};
const data1: EventObject<Data1>[] = [
{
id: 1,
a: 1,
b: "string",
},
{
id: 2,
a: 2,
b: "string",
},
];

const data2: EventObject<Data2>[] = [
{
id: 1,
a: 1,
c: "string",
},
{
id: 2,
a: 2,
c: "string",
},
];

export const data = {
data1: { data: data1 },
data2: {
data: data2,
},
} as const;

export const config = defineConfig<typeof data>({
data1: {
cell: (event) => {}, //event should be EventObject<Data1>
},
data2: {
cell(event) {}, //event should be EventObject<Data2>
},
});
import { defineConfig, type EventObject } from "../types";
type Data1 = {
b: string;
};
type Data2 = {
c: string;
};
const data1: EventObject<Data1>[] = [
{
id: 1,
a: 1,
b: "string",
},
{
id: 2,
a: 2,
b: "string",
},
];

const data2: EventObject<Data2>[] = [
{
id: 1,
a: 1,
c: "string",
},
{
id: 2,
a: 2,
c: "string",
},
];

export const data = {
data1: { data: data1 },
data2: {
data: data2,
},
} as const;

export const config = defineConfig<typeof data>({
data1: {
cell: (event) => {}, //event should be EventObject<Data1>
},
data2: {
cell(event) {}, //event should be EventObject<Data2>
},
});
What should the TODO type be so that the callback prop event knows that it is the event of the current property so in data1 event is EventObject<Data1> and in data2 it is EventObject<Data2>
7 replies