Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-extractor] Generate .d.ts with fully compiled and inlined types #5032

Open
doteric opened this issue Dec 6, 2024 · 5 comments
Open

Comments

@doteric
Copy link

doteric commented Dec 6, 2024

Summary

I would like to be able to generate a fully compiled and inlined .d.ts with only the exported types of the main file.

Details

Let's say I have an input file of:

export type { ResponseBodyType as ResultResBodyType } from '../result/index.js';
export type { BodyType as ResultReqBodyType } from '../result/modules/eventParser.js';

And want simply just:

// Just an example
export type ResultResBodyType =
  | { issues: {message: string, fatal: boolean}[]; message?: undefined; result?: undefined }
  | { message: string; issues?: undefined; result?: undefined }
export type ResultReqBodyType = { example: string };

Without any external libraries being put inside the declaration file. I want all the external things to be compiled.
The above would contain compiled types like usage of zod for example.

Currently instead I get a copy of the actual code.. Example:

import type { APIGatewayProxyEvent } from 'aws-lambda';
import type { Image } from '@aws-sdk/client-rekognition';
import { z } from './lib';

declare const handlerBodyZod = z.object({
    accountNumber: z
    .string()
    .regex(ACCOUNT_REGEX, { message: 'Invalid AccountNumber Format' }),
});

declare const main = async (event: APIGatewayProxyEvent) => {
  // Code of the whole main function
};

export declare type ResultReqBodyType = z.infer<typeof handlerBodyZod>;

export declare type ResultResBodyType = Awaited<ReturnType<typeof main>>['body'];

export { }

Thank you and really appreciate the work on the tool! 🙇

@doteric
Copy link
Author

doteric commented Dec 12, 2024

@iclanton sorry for the ping, but would you mind taking a look if you would be able to introduce such feature in the future by any chance or no plans to do so? Appreciate it 🙇

@JeongJuhyeon
Copy link

@doteric

We're facing the same, so very curious what you've ended up going with! Just like you, I'm seeing that certain libraries such as Zod do not get inlined at all, remaining as import { z } from './lib'; even when explicitly added to bundledPackages in the config. I'm struggling to understand how this import makes sense; there is no "./lib" exporting z so how should this work?

With certain other libraries it does seem to work, so the unexpected behaviour with zod may just be something particular to certain large libraries. However..

If those libraries' types use types that are imported from yet other libraries, it still doesn't seem to work.
Example: if our code uses a type from library A, which in turn depends on library B, the types of library A will generally get inlined, but the types of library B that it uses will get added as imports.

@doteric
Copy link
Author

doteric commented Dec 30, 2024

Hey @JeongJuhyeon

I'm using a custom made script for now which is awful and only does some of what I need, so the best thing would be to get attention from one of the maintainers and see what they have to say about this. Addressing this issue and adding the ability to fully inline all types would be an awesome feature.

@JeongJuhyeon
Copy link

JeongJuhyeon commented Jan 2, 2025

Hey @JeongJuhyeon

I'm using a custom made script for now which is awful and only does some of what I need, so the best thing would be to get attention from one of the maintainers and see what they have to say about this. Addressing this issue and adding the ability to fully inline all types would be an awesome feature.

We have ended up succeeding by using the dts-bundle-generator package, I wrote a bit about it here: timocov/dts-bundle-generator#346

In the package we're looking to run the bundler on, we install the the packages with the types as devdependencies and the following bundler.config.json:

{
    "compilationOptions": {
        "preferredConfigPath": "./tsconfig-bundler.json",
        "followSymlinks": true
    },
    "entries": [
        {
            "filePath": "./src/index.ts",
            "outFile": "./dist/index.d.ts",
            "libraries": {
                "inlinedLibraries": [
                    // here go all of the external deps we want to inline, you can look at the output and put the ones that still get imported here
                ]
            },
            "output": {
                "inlineDeclareGlobals": true,
                "exportReferencedTypes": false
            }
        }
    ]
}

And the following tsconfig-bundler.json:

{
    "extends": "../../tsconfig-base.json",
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "*/dist",
        "dist",
        "node_modules"
    ],
    "compilerOptions": {
        "module": "NodeNext",
        "moduleResolution": "nodenext",
        "outDir": "dist-bundler",
        "types": [
            "node"
        ]
    },
    "paths": {
        "backend": [
            "../backend"
        ]
    }
}

And then the trick was to, when importing from the internal backend package, do so from the compiled .d.ts files, e.g.

import type { Provider } from "backend/dist/provider/provider.d.ts";

This way everything gets inlined if you desire so.

@doteric
Copy link
Author

doteric commented Jan 2, 2025

@JeongJuhyeon
I actually tried using dts-bundle-generator also, but it wasn't ideal either.
I created this issue: timocov/dts-bundle-generator#338
The author does not want to fully inline types to only the necessary and when for example zod is imported it bloats the file quite a big bit which preferably I would like to avoid. Thanks for the suggestion though 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Needs triage
Development

No branches or pull requests

2 participants