The problem here is what is the return type of runtime.objects[str]
? For example suppose you have a Sprite and a Text object in your project. Will runtime.objects[str].getFirstInstance()
return a Sprite instance (with animation APIs) or a Text instance (with text APIs)? TypeScript doesn't know, even if you persuade it that using str
is valid.
If there is only a small number of possible objects that may be created, it may be better to just use a series of if
statements comparing the string and creating the right kind of object. If it could be anything at all, then your approach using any
is probably all you can do, as you are telling TypeScript basically anything could happen there.