I'm in the process of creating a function that accepts a specific filter key
with a value
that corresponds to a keyof
an interface
I've defined. The desired outcome is to return an object
where the filter value
acts as one of the keys
and its type from the interface serves as the corresponding value.
Despite experimenting with different typings, my lack of understanding in TypeScript hinders me from solving this task. Below is my attempted implementation:
Interface
interface MyModel {
a: string;
b: number;
c: boolean;
}
Function
function myfunction(filter: keyof MyModel): {
date: Date;
[key: data.filter]: typeof filter
} {
// ...
}
The above function is flawed, but it showcases the concept I'm aiming for. Currently, my workaround involves using Partial<MyModel>
, which functions but requires null checking for the value as demonstrated below:
function myfunction(filter: keyof MyModel): Partial<MyModel> & {
date: Date;
} {
// ...
}
Is there a way to refine the typings as shown in the example above?