#Executing a function from a string using dynamic import
155 messages · Page 1 of 1 (latest)
!resolve
Actually, my bad, my idea was to instead return something else in the function but forgot I won't still be able to call it
!reopen
Do you mean something like
const level = "debug";
const support = await import("../constants/support");
support[level](parameter);
I'll test out in a couple of minutes
Actually, my bad, no
I have a parameter
For example
const parameter = "debug";
And I need to run the parameter via support.
@lapis knoll while what you're trying to do might work, I would not recommend it
running code from a string is not ideal
also the fact the code is a strig doesn't give you any type checking, etc.
Hey, so I made a list string[], if the parameter is not in the array, then error
Ok well, so if debug is not in a list
const list = ["notdebug", "a"];```
then error, if notdebug is the parameter, run notdebug
!helper
what
Something like this https://stackoverflow.com/a/62455421, but I don't know how to implement it in my situation
No index signature with a parameter of type 'string' was found on type 'typeof import .....
In the const file I'm using EmbedBuilder class and cannot be a string
many ways to fix
Well do you have any ideas?
one way would be to use as any there 🧠
other proper way would to use a type guard function
you are reading the error wrong
Probably I can convert the const to a function, but would that still work the same?
what const
I mean this works
const support = await import("../constants/support") as any;
[support[parameter]()]```
just haven't executed the code to test
export const notdebug = () => {}```
Well Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'typeof import ....
its already a function?
Oh, then my bad
If I make the import as any it only works that way, I'll go ahead and try to execute the code
btw does support file export the same function signatures but different functionalities?
Well that worked perfectly
What do you mean?
Ah well if parameter === "notdebug", then it would run export const notdebug = () => {}
But still, is it alright to make the import as any?
if you are asking this question you should have written it in javascript
imo, no, and ill never do that
Well do you have any suggestions on how would I make it not any? I mean (support[parameter] as String) thought going to be fine but no
support[parameter as any]
Yea well going back to ^
would help marginally
that assumes this brw
btw*
Currently confused, I guess you mean is my function has an any type? If so, no
no like,
export const debug = (msg: string) => {}
export const warn = (msg: string) => {}
export const info = (msg: string) => {}
I got export const debug = (guild: Guild) => {}, so I did support[parameter as any](interaction.guild)
Would this be my solution?
const support = await import("../constants/support") as string;
support[parameter as string](interaction.guild)```
if so, I got 2 errors, first one VSC tells me to do `import("...") as unknown as string`, second `Element implicitly has an 'any' type because index expression is not of type 'number'.` on parameter, but I've did `let parameter: string` also
!helper
whats the problem now
are you asking about your solution?
or are you looking for a solution to the original problem
Currently both, but I guess the best solution is my current solution, but it needs fixing
@lapis knoll is this someone elses code base
What do you mean?
how do you usually fix ts errors
Well such as?
variable 's' is type unknown
Well in most solutions I would just make sure the string is required, but I've never used dynamic imports
Oh well in this example making the const string worked, but import is not assignable tho, basically the same thing
this isn't a 100% correct representation but you can think of a module as an object
so await import("...") would be an object
if you are doing support[parameter as string], it has the same effect as SomeType[string] and that will be the union of all possible members
but there are two catches
(typeof support)[string] is not guaranteed to be valid since string is not limited to keyof support
so
lets say you convince TS that it is using as any
support[parameter as any]
the value of that will be the union of all valid members
if one of the members are different from another, it will be tricky for you to call
or, you can ignore this using // @ts-ignore and come back later
Well parameter doesn't currently matter, because if you even make the import as any, it would work, but not ideal, so if I would need to make the import as a type which I currently don't know, so I'm here
well, i suggest making a typeguard
for parameter
when starting out typescript it can be hard
and often times somethings are obvious while others need more understanding of the comiler and language itself to fully comprehend
its good that you are trying to get things right but right now, its a little hard for you to do that
you might end up in a rabbit hole and never finish your project
so, try focus on making it work. this won't be your last project anyway, so at least show that it works before realising what you can do better
Well parameter is string by
let parameter: string = interaction.options.getString("parameter", true); // could return anything, such as "notdebug"```
using let because I've made
```ts
if (parameter.includes("/"))
parameter = parameter.replace("/", "_");```
But the error is happening on import, because it cannot be a typeof string, so I'm here asking, what should it be?
do you know what the parameter can be
actually, whats the most likely string it can be
Well d.js also has options when calling an interaction with slash commands in my example, so getString is
public getString(name: string, required: true): string;
no like
when the code actually runs support[parameter]
what is parameter
most likely going to be
im trying to help you monkey patch this
It could be anything that users can type, if the string is notdebug in the parameter, then I would simply need support.notdebug()
what if user does
asdf
its gonna call
support.asdf()
and your bot will crash because undefined is not a function
Well if asdf is in a string[], then yes
const list = ["notdebug", "somethingrandom"];
if (!list.includes(parameter))
return throw new Error("literally an example");```
I'm not actually using throw, just returning an interaction.reply
const list = ["notdebug", "somethingrandom"] as const;
type ValidMethod = (typeof list)[number];
function isValidMethod(method: string): method is ValidMethod {
return list.includes(method);
}
Right, but Argument of type 'T' is not assignable to parameter of type "'notdebug ... and T cannot be used as a type in a value
oh crap
there
Understand how TypeScript uses JavaScript knowledge to reduce the amount of type syntax in your projects.
read this
Well I did
function isValidFunction(method: string): method is ValidMethod {
return list.includes(method);
}``` removing the <>, but now method in the .includes is somehow not assingable to `"notdebug" | "somethingrandom"'`
where
mb edited the message
oh um, wow typescript got better
change the code to this
type ValidMethod = "notdebug" | "somethingrandom";
const list = ["notdebug", "somethingrandom"] satisfies ValidMethod[];
function isValidMethod(method: string): method is ValidMethod {
return list.includes(method);
}
basically, they improved the type system of builtins i think
correct me if im wrong, others
anyway, this way list is string[] and the code should work
That didn't work, but I'm going to change the list too, when going into reviewing, also VSC formatting making me do this ]satisfies
Same error, TS 4.9.5
oh myb
const list: string[] = ["notdebug", "somethingrandom"] satisfies ValidMethod[];
isValidFunction will help you narrow down parameter to either of those strings
and typescript won't complain about doing support[parameter]
Unfortunatly it does complain about ^
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof ...
No index signature with a parameter of type 'string' was found on type 'typeof ...```
on support[parameter]
whats support
const list: string[] = ["notdebug", "somethingrandom"]satisfies ValidMethod[];
type ValidMethod = "notdebug" | "somethingrandom";
function isValidMethod(method: string): method is ValidMethod {
return list.includes(method);
}
if (!isValidMethod(parameter))
return interaction.reply(...);
if (parameter.includes("/"))
parameter = parameter.replace("/", "_");
const support = await import("../constants/support");
....... support[parameter]() ......```
That does nothing new, basically just remove the ! and makes the code switch places, tried it, same error on support[parameter]
its going to help you catch bugs later anyway
how are you exporting the functions btw
in the support file
my bad, ^
Using ES2022 if matters, anything else I can provide
So, anyone got some suggestions?