Import .d.ts in ts file

407 views Asked by At

I have an issue. I am trying to import some types from a .d.ts file that I have created but ts shows error on the path of the file with message:

File '.../lib/types/generated.d.ts' is not a module.ts(2306)

The line of the error is at the start of a .ts file:

import type { CategoryForm } from '../../lib/types/generated';

And the file contains:

...
declare namespace App.DTO.Category {
export type CategoryData = {
id: string;
parent_id: string | null;
name: string;
slug: string;
category_type: App.Enums.CategoryType;
created_at: string;
updated_at: string;
};
export type CategoryForm = {
parent_id: string | null;
name: string;
category_type: App.Enums.CategoryType;
};
}
...

What am I doing wrong?

Tried to import the type with as many ways as possible but I couldn't.

1

There are 1 answers

0
Kylian Lion On

The error you're encountering, "File '.../lib/types/generated.d.ts' is not a module.ts(2306)," typically occurs when TypeScript expects a module but doesn't find one in the specified path. In your case, the .d.ts file should explicitly declare a module if it's being imported as one.

Declare the File as a Module, in your generated.d.ts file, wrap your declarations within a module declaration. This explicitly tells TypeScript that this file is a module. For example:

declare module 'yourModuleName' {
  export namespace App.DTO.Category {
    // ... your types here ...
  }
}