Skip to content

Commit ab97a70

Browse files
erunionkanadgupta
andauthored
fix: resolving typing issues in JS codegen (#593)
* fix: stop exporting types in the main entrypoint for JS projects * fix: update CJS exports to have `export =` in their `.d.ts` files * fix: linting issue * Update packages/api/src/cli/codegen/languages/typescript.ts Co-authored-by: Kanad Gupta <[email protected]> Co-authored-by: Kanad Gupta <[email protected]>
1 parent bf57315 commit ab97a70

File tree

3 files changed

+32
-38
lines changed

3 files changed

+32
-38
lines changed

packages/api/src/cli/codegen/languages/typescript.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,22 +172,29 @@ export default class TSGenerator extends CodeGeneratorLanguage {
172172
this.createSchemasFile();
173173
this.createTypesFile();
174174

175-
// Export all of our available types so they can be used in SDK implementations.
176-
//
177-
// We're exporting all of the types individually because TS has no way right now of allowing
178-
// us to do `export type * from './types'` on a non-named entry.
179-
//
180-
// https://github.com/microsoft/TypeScript/issues/37238
181-
const types = Array.from(this.types.keys());
182-
types.sort();
183-
184-
sdkSource.addExportDeclarations([
185-
{
186-
isTypeOnly: true,
187-
namedExports: types,
188-
moduleSpecifier: './types',
189-
},
190-
]);
175+
/**
176+
* Export all of our available types so they can be used in SDK implementations. Types are
177+
* exported individually because TS has no way right now of allowing us to do
178+
* `export type * from './types'` on a non-named entry.
179+
*
180+
* Types in the main entry point are only being exported for TS outputs as JS users won't be
181+
* able to use them and it clashes with the default SDK export present.
182+
*
183+
* @see {@link https://github.com/microsoft/TypeScript/issues/37238}
184+
* @see {@link https://github.com/readmeio/api/issues/588}
185+
*/
186+
if (!this.outputJS) {
187+
const types = Array.from(this.types.keys());
188+
types.sort();
189+
190+
sdkSource.addExportDeclarations([
191+
{
192+
isTypeOnly: true,
193+
namedExports: types,
194+
moduleSpecifier: './types',
195+
},
196+
]);
197+
}
191198
} else {
192199
// If we don't have any schemas then we shouldn't import a `types` file that doesn't exist.
193200
sdkSource
@@ -406,7 +413,14 @@ sdk.server('https://eu.api.example.com/v14');`)
406413
],
407414
});
408415

409-
sourceFile.addExportAssignment({ isExportEquals: false, expression: 'createSDK' });
416+
sourceFile.addExportAssignment({
417+
// Because CJS targets have `createSDK` exported with `module.exports`, but the TS type side
418+
// of things to work right we need to set this as `export =`. Thankfully `ts-morph` will
419+
// handle this accordingly and still create our JS file with `module.exports` and not
420+
// `export =` -- only TS types will have this export style.
421+
isExportEquals: this.compilerTarget === 'cjs' && this.outputJS,
422+
expression: 'createSDK',
423+
});
410424

411425
return sourceFile;
412426
}

packages/api/test/__fixtures__/sdk/simple-js-cjs/index.d.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,4 @@ declare class SDK {
6666
): Promise<FetchResponse<200, types.FindPetsByStatusResponse200>>;
6767
}
6868
declare const createSDK: SDK;
69-
export default createSDK;
70-
export type {
71-
ApiResponse,
72-
Category,
73-
FindPetsByStatusMetadataParam,
74-
FindPetsByStatusResponse200,
75-
Order,
76-
Pet,
77-
Tag,
78-
User,
79-
} from './types';
69+
export = createSDK;

packages/api/test/__fixtures__/sdk/simple-js-esm/index.d.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,3 @@ declare class SDK {
6767
}
6868
declare const createSDK: SDK;
6969
export default createSDK;
70-
export type {
71-
ApiResponse,
72-
Category,
73-
FindPetsByStatusMetadataParam,
74-
FindPetsByStatusResponse200,
75-
Order,
76-
Pet,
77-
Tag,
78-
User,
79-
} from './types';

0 commit comments

Comments
 (0)