launchdarkly / js-core

LaunchDarkly monorepo for JavaScript SDKs
Other
12 stars 12 forks source link

fix(types): LDClient.waitForInitialization resolves this #459

Closed SevenOutman closed 1 month ago

SevenOutman commented 1 month ago

Requirements

Related issues

https://github.com/launchdarkly/js-core/issues/324

Describe the solution you've provided

Type the return type with this keyword

Describe alternatives you've considered

Additional context

In my use case, the problem was that the return type of waitForInitialization is not compatible with ld.LDClient

import * as ld from '@launchdarkly/node-server-sdk';

export function initLdClient(): Promise<ld.LDClient> {
  return ld.init(process.env.LAUNCH_DARKLY_SDK_KEY as string)
           .waitForInitialization(); // Type error
}
chadxz commented 1 month ago

In the docs for the waitForInitialization method, it says:

It is not recommended to use the returned client object. It will be removed in a future version.

FYI.

kinyoklion commented 1 month ago

Hello @SevenOutman,

As @chadxz mentioned we have updated the docs to indicate the return value here will be removed in the future.

Some previous discussion: https://github.com/launchdarkly/js-core/issues/324

I would recommend something like this instead.

export async function initLdClient(): Promise<ld.LDClient> {
  const client =  ld.init(process.env.LAUNCH_DARKLY_SDK_KEY as string);
  await client.waitForInitialization();
  return client;
}

(The fix is nice though.)

Thank you, Ryan

SevenOutman commented 1 month ago

Understood. Thanks both! I'm closing the PR.