I wrote some typescript code and I am not quiet sure why it is not working as I expected. I would really appriopriate if someone would explain me what I do wrong or what other approach should I use.
Let's say I have a Definition of a function name and data.
interface Definition<TName extends string, TData extends any> {
name: TName;
data: TData;
}
Definition at some point changes into Action. Action has a function named method with payload as first paramater type.
interface Action<TDefinition extends Definition<string, any>> {
name: TDefinition["name"];
method: (data: TDefinition["data"]) => void;
}
Keep in mind that all Definition union elements must move through Changer.
interface Changer<TDefinition extends Definition<string, any>> {
actions: Action<TDefinition>[];
}
interface Definition<TName extends string, TData extends any> {
name: TName;
data: TData;
}
interface Action<TDefinition extends Definition<string, any>> {
name: TDefinition["name"];
method: (data: TDefinition["data"]) => void;
}
interface Changer<TDefinition extends Definition<string, any>> {
actions: Action<TDefinition>[];
}
const set: Changer<Definition<"one", 1> | Definition<"two", 2>> = {
actions: [
{
name: "one",
method: (data) => data, // data type is equal to '1 | 2' I would expect it to be '1'
},
],
};
set.actions[0].method(1); // data type is equal to '1 | 2' I would expect it to be '1'
Could you tell me what should I do to achieve result I expect?
I am thinking new Definition, however I belive I may have some issues because of [key: string]: any.
interface Definition<TName extends string, TData extends { [key: string]: any }> {
[key in TName]: TData[key]
}
What do you think?
set?setis just a variable name - it does not matter, there is no more meaning behind it