Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix common package to work with node16 module resolution. (#627)
When using typescript with moduleResolution set to "node16" types were not loading which are part of the common package. This relates to changes to the default was in which file resolution occurs. This PR will address this, but we will want a better long-term fix. In this PR we convince node that the `.d.ts` files are the correct extension by making a small `package.json` that sets the module type. In the future we would want the `.d.ts` files to instead be `.d.cts`. Potentially we can move to tsup: https://tsup.egoist.dev/ But the changes were too extensive for this fix. Testing: tsconfig.json ``` { "$schema": "https://json.schemastore.org/tsconfig", "_version": "20.1.0", "compilerOptions": { "lib": ["es2023"], "module": "node16", "target": "es2022", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "moduleResolution": "node16" } } ``` package.json ``` { "name": "hello-node-typescript", "version": "1.0.0", "description": "Hello LaunchDarkly for Node.js with TypeScript", "main": "index.ts", "scripts": { "start": "ts-node ./index.ts" }, "author": "LaunchDarkly <[email protected]>", "license": "Apache-2.0", "devDependencies": { "@types/node": "*", "ts-node": "*", "typescript": "*" }, "dependencies": { "@launchdarkly/node-server-sdk": "../js-core/packages/sdk/server-node", "@launchdarkly/js-server-sdk-common": "../js-core/packages/shared/sdk-server", "@launchdarkly/js-sdk-common": "../js-core/packages/shared/common" } } ``` Code: ``` import { init } from '@launchdarkly/node-server-sdk'; import type { LDSingleKindContext, LDLogger, LDOptions } from '@launchdarkly/node-server-sdk'; // Set sdkKey to your LaunchDarkly SDK key. const sdkKey = ""; function showMessage(s: string) { console.log("*** " + s); console.log(""); } const logger: LDLogger = { debug: console.debug, info: console.info, warn: console.warn, error: console.error, }; const options: LDOptions = { logger }; const client = init(sdkKey, options); client.once('ready', async function () { showMessage("SDK successfully initialized!"); const context: LDSingleKindContext = {kind: 'user', key: 'bob'}; const res = await client.variation('my-boolean-flag', context, false); console.log("The result", res); }); ``` This uses a few types, such as the context, logger, and options to ensure they are resolving correctly.
- Loading branch information