Skip to content

Conversation

@mhelaiwa
Copy link
Contributor

Rename generated types file by adding -types to resolve issue discussed here #702

@bakerkretzmar bakerkretzmar self-assigned this Mar 19, 2025
@bakerkretzmar
Copy link
Collaborator

What is the actual underlying issue here? Do you know why this fixes the issues in that discussion thread?

@mhelaiwa
Copy link
Contributor Author

mhelaiwa commented Mar 19, 2025

@bakerkretzmar
import { Ziggy as routes } from "@/js/router/ziggy.js"
Give a ts error: Vue: Module "@/js/router/ziggy. js" has no exported member Ziggy
This error shows only when the 2 files have the same name .
Somehow TS is looking for Ziggy module in ziggy.d.ts not ziggy.js

@bakerkretzmar
Copy link
Collaborator

Do you know why this is a problem or what's actually causing it?

@mhelaiwa
Copy link
Contributor Author

@bakerkretzmar Unfortunately no. I've tried all solutions I have found so far but the problem won't be solved until I rename the file. It could be an editor-related issue, but I think it's not a big deal to generate types in a different named file :)

@bakerkretzmar
Copy link
Collaborator

What is in your @/js/router/ziggy.js file and how/where exactly are you generating your types?

@bram-pkg
Copy link
Contributor

This is an issue because typescript thinks that ziggy.d.ts is the type definition file for ziggy.js.

They are however completely unrelated, and ziggy.js defines the routes, whereas ziggy.d.ts defines the types for the ziggy-js module, resulting in a failing import because ziggy.d.ts does not export a Ziggy member, but ziggy.js does export { Ziggy }.

Renaming ziggy.d.ts to ziggy-module.d.ts or the like, would fix this issue.

With the same name:

image

Without the same name:

image

$types = config('ziggy.output.types', Types::class);

$filesystem->put(base_path("{$name}.d.ts"), new $types($ziggy));
$filesystem->put(base_path("{$name}-types.d.ts"), new $types($ziggy));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$filesystem->put(base_path("{$name}-types.d.ts"), new $types($ziggy));
$filesystem->put(base_path("{$name}-module.d.ts"), new $types($ziggy));

file_put_contents(
base_path('resources/js/ziggy.d.ts'),
preg_replace('/\r?\n/', "\r\n", file_get_contents(base_path('resources/js/ziggy.d.ts'))),
base_path('resources/js/ziggy-types.d.ts'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
base_path('resources/js/ziggy-types.d.ts'),
base_path('resources/js/ziggy-module.d.ts'),

base_path('resources/js/ziggy.d.ts'),
preg_replace('/\r?\n/', "\r\n", file_get_contents(base_path('resources/js/ziggy.d.ts'))),
base_path('resources/js/ziggy-types.d.ts'),
preg_replace('/\r?\n/', "\r\n", file_get_contents(base_path('resources/js/ziggy-types.d.ts'))),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
preg_replace('/\r?\n/', "\r\n", file_get_contents(base_path('resources/js/ziggy-types.d.ts'))),
preg_replace('/\r?\n/', "\r\n", file_get_contents(base_path('resources/js/ziggy-module.d.ts'))),

}

expect(base_path('resources/js/ziggy.d.ts'))->toEqualFile('./tests/fixtures/ziggy.d.ts');
expect(base_path('resources/js/ziggy-types.d.ts'))->toEqualFile('./tests/fixtures/ziggy.d.ts');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(base_path('resources/js/ziggy-types.d.ts'))->toEqualFile('./tests/fixtures/ziggy.d.ts');
expect(base_path('resources/js/ziggy-module.d.ts'))->toEqualFile('./tests/fixtures/ziggy.d.ts');

artisan('ziggy:generate --types-only');

expect(base_path('resources/js/ziggy.d.ts'))->toBeFile();
expect(base_path('resources/js/ziggy-types.d.ts'))->toBeFile();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(base_path('resources/js/ziggy-types.d.ts'))->toBeFile();
expect(base_path('resources/js/ziggy-module.d.ts'))->toBeFile();

Copy link
Contributor

@bram-pkg bram-pkg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think -module is a better prefix, because the file contains the module declaration for ziggy-js.


expect(base_path('resources/js/custom.d.ts'))->toBeFile();
expect(base_path('resources/js/ziggy.d.ts'))->not->toBeFile();
expect(base_path('resources/js/custom-types.d.ts'))->toBeFile();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(base_path('resources/js/custom-types.d.ts'))->toBeFile();
expect(base_path('resources/js/custom-module.d.ts'))->toBeFile();

expect(base_path('resources/js/custom.d.ts'))->toBeFile();
expect(base_path('resources/js/ziggy.d.ts'))->not->toBeFile();
expect(base_path('resources/js/custom-types.d.ts'))->toBeFile();
expect(base_path('resources/js/ziggy-types.d.ts'))->not->toBeFile();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(base_path('resources/js/ziggy-types.d.ts'))->not->toBeFile();
expect(base_path('resources/js/ziggy-module.d.ts'))->not->toBeFile();

Comment on lines +156 to +160
['resources/js/x.js --types', ['resources/js/x.js', 'resources/js/x-types.d.ts']],
['resources/js/y.ts --types', ['resources/js/y.js', 'resources/js/y-types.d.ts']],
['resources/js/z.d.ts --types', ['resources/js/z.js', 'resources/js/z-types.d.ts']],
['resources/scripts/foo --types', ['resources/scripts/foo.js', 'resources/scripts/foo-types.d.ts']],
['resources/js --types', ['resources/js/ziggy.js', 'resources/js/ziggy-types.d.ts']],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
['resources/js/x.js --types', ['resources/js/x.js', 'resources/js/x-types.d.ts']],
['resources/js/y.ts --types', ['resources/js/y.js', 'resources/js/y-types.d.ts']],
['resources/js/z.d.ts --types', ['resources/js/z.js', 'resources/js/z-types.d.ts']],
['resources/scripts/foo --types', ['resources/scripts/foo.js', 'resources/scripts/foo-types.d.ts']],
['resources/js --types', ['resources/js/ziggy.js', 'resources/js/ziggy-types.d.ts']],
['resources/js/x.js --types', ['resources/js/x.js', 'resources/js/x-module.d.ts']],
['resources/js/y.ts --types', ['resources/js/y.js', 'resources/js/y-module.d.ts']],
['resources/js/z.d.ts --types', ['resources/js/z.js', 'resources/js/z-module.d.ts']],
['resources/scripts/foo --types', ['resources/scripts/foo.js', 'resources/scripts/foo-module.d.ts']],
['resources/js --types', ['resources/js/ziggy.js', 'resources/js/ziggy-module.d.ts']],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants