I have a sugary javascript SDK where the library is instantiated at any given point at runtime, but from then onwards is a singleton. As in, repeat calls to the default
of the file return the same instance.
My problem is that my typings are lost between the definition of the library and the instantiation.
See this code:
const Factory = (args?: Record<string, unknown>) => {
return {
action: {
method: (code: 'SOME'|'CODES') => console.log('Doing something with', code)
}
}
}
let instance
export default (args?: Record<string, unknown>) => {
if (!instance)
instance = Factory(args)
return instance
}
If I instantiate the Factory
directly, I'm furnished with typings correctly. However, the exported interface for the file is attempting to ensure the instance always returns a singleton. In that process we loose typings.
How should I type instance
here, in order to retain the typings?
Constraints:
- I don't want to have to re-type everything in an interface that loosely matches the definition
- The initial instance can be instantiated by the user at any point during runtime (cannot be assumed to be created on bootstrap)
- Shouldn't use classes due to the potential complexity of the nested methods that are returned.
CodePudding user response:
You can use the ReturnType
utility type to get the inferred type of the instance that Factory
returns, then use that in the type of instance
and the return type of the default export. See LibInstanceType
below:
const Factory = (args?: Record<string, unknown>) => {
return {
action: {
method: (code: 'SOME'|'CODES') => console.log('Doing something with', code)
}
}
};
type LibInstanceType = ReturnType<typeof Factory>;
let instance: LibInstanceType | undefined;
export default (args?: Record<string, unknown>): LibInstanceType => {
if (!instance)
instance = Factory(args);
return instance;
};