I don't fully understand the OP question but I think this will certainly help some people searching for inverse of typeof
which is how I got here myself.
I have a list of dynamic components in a lookup table.
class ImageComponent
{
media: 'IMAGE';
}
class VideoComponent
{
media: 'VIDEO';
}
// lookup table (runtime construct)
const components = {
'image': ImageComponent,
'video': VideoComponent
}
So I want to take 'image'
(the key in the table) and end up with IMAGE
which is a property of ImageComponent
.
Lookup the component from the table:
type imageComponent = typeof components['image']; // typeof ImageComponent
If we actually had ImageComponent
then we could do a type lookup on it.
ImageComponent['media'] // 'IMAGE'
But we have typeof ImageComponent
instead which is useless for finding any properties on the class.
type mediaType = imageComponent['media']; // does not work
The actual answer...
So what we can do is get the 'prototype type' from something that is `typeof YourClassName.
type media = imageComponent['prototype']['media']; // 'IMAGE' (const value)
or in other words what we're actually doing is:
type media = (typeof ImageComponent)['prototype']['media'];
So for me this satisfied my search for 'inverse of typeof'.
Also note that 'IMAGE'
and 'VIDEO'
are literal types and not strings (except at runtime when they are just strings).