#Type Declaration File: How do I make it without a wildcard?

11 messages · Page 1 of 1 (latest)

patent minnow
#

I have a TOML type declaration file, however I want to make it only match config.toml not *.toml, however if I change the module declaration, it shows the error: Cannot find module '../../config.toml' or its corresponding type declarations.

declare module "*.toml" {
    const config: {
        settings: {
            logLevel: "none" | "normal" | "verbose";
        },

        addons: {
            sentry: string;
        }
    };

    export default config;
}
drowsy moss
#

how are you importing that file? as in, are you using any tooling to support that import?

patent minnow
#

I'm using the bun runtime

#

the file is being handled already by bun, it's just the intellisense that is erroring

#

here's an example

import * as _Sentry from "@sentry/bun";
import config from "../../config.toml";

export const Addons = {
    Sentry,
};

function Sentry(): typeof _Sentry {
    _Sentry.init({
        dsn: config.addons.sentry,
        tracesSampleRate: 1.0,
        environment: Bun.env.NODE_ENV ?? "development",
    });

    return _Sentry;
}
drowsy moss
#

declare module for foreign file extensions is typically to show what shape that kind of file will have on import, i don't know if ts cares about the exact import path on that

patent minnow
#

Like I said, with *.toml it works fine, no intellisense error, however that means all TOML files must conform to my config typing
I want the typing to only match config.toml so other toml files can have their own typing.

Without the .d.ts file, there is a permanent intellisense error saying unable to find module, however during runtime this error doesn't exist as it's valid in bun.

I'm just trying to avoid the intellisense error whilst adding typing to the object imported

drowsy moss
#

i feel like declare module "config.toml" would be looking for that import exactly, rather than just the file name, could you maybe check if that's getting the declarations instead?

#

and using a path in declare module just errors, so i don't think it can specify specific files.

drowsy moss
#

you can still use like, / or # in the module name, so i think you could use subpath imports or something like that to get it to work