shennan
shennan

Reputation: 11666

TypeScript singleton generated with a factory function looses typings

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:

Upvotes: 1

Views: 323

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075069

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;

};

Playground link

Upvotes: 1

Related Questions