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

Allow exporting of types from module script #2555

Open
tzuleger opened this issue Oct 24, 2024 · 5 comments
Open

Allow exporting of types from module script #2555

tzuleger opened this issue Oct 24, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@tzuleger
Copy link

Describe the problem

In Svelte <=4, it was possible to export JSDOC custom types through the context="module" <script> tag by simply declaring the type.

In Svelte 5, it seems that types defined in the module script are not exported. At least, there's no clear way of doing it or no documentation on it.

Describe the proposed solution

Allow users to export types from the module script, to be imported from anywhere in the project.

$lib/components/MyComponent.svelte:

<script module>
/** @typedef {"myType"} MyType */
</script>
<script>
</script>

<!-- stuff -->

<style>
</style>

$lib/util/misc.js:

/** @import { MyType } from "$lib/components/MyComponent.svelte" */

/**
 * @param {MyType} x
 */
function stuff(x) {
  
}

Importance

would make my life easier

@dummdidumm
Copy link
Member

This should still work. Make sure you are on the latest version of svelte-check and the Svelte extension of your IDE

@tzuleger
Copy link
Author

I am on the latest version of svelte-check. Here's my package.json for proof:

{
	"name": "zod",
	"version": "0.0.1",
	"scripts": {
		"dev": "vite dev",
		"build": "vite build && npm run package",
		"preview": "vite preview",
		"package": "svelte-kit sync && svelte-package && publint",
		"prepublishOnly": "npm run package",
		"check": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json",
		"check:watch": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json --watch"
	},
	"exports": {
		".": {
			"types": "./dist/index.d.ts",
			"svelte": "./dist/index.js"
		}
	},
	"files": [
		"dist",
		"!dist/**/*.test.*",
		"!dist/**/*.spec.*"
	],
	"peerDependencies": {
		"svelte": "^5.0.0-next.1"
	},
	"devDependencies": {
		"@sveltejs/adapter-auto": "^3.0.0",
		"@sveltejs/kit": "^2.0.0",
		"@sveltejs/package": "^2.0.0",
		"@sveltejs/vite-plugin-svelte": "^3.0.0",
		"publint": "^0.1.9",
		"svelte": "^5.0.0-next.1",
		"svelte-check": "^4.0.5",
		"tslib": "^2.4.1",
		"typescript": "^5.0.0",
		"vite": "^5.0.11"
	},
	"svelte": "./dist/index.js",
	"types": "./dist/index.d.ts",
	"type": "module",
	"dependencies": {
		"zod": "^3.23.8"
	}
}

Unfortunately, it still doesn't work. It works just fine with regular exports.

Here's my module script:

<script module>
    /**
     * @typedef {"test"} TestType 
     */
    export const types = {};
</script>

Here's my JS file:

//@ts-check
/** @import { ZodIssue } from "zod" */
/** @import { TestType, types } from "$lib/components/ZodInput.svelte" */

/** @type {TestType} */
const x = "";

Unfortunately, Intellisense is not picking up TestType. Here's a screenshot of VSCode's intellisense yelling at me:

image

@dummdidumm
Copy link
Member

Which version of the Svelte vs code extension do have installed?

@tzuleger
Copy link
Author

tzuleger commented Oct 26, 2024

I'm on the latest, v109.1.0
image

@hfjallemark
Copy link

I'm seeing the same issue in latest Zed Preview.

@dummdidumm dummdidumm transferred this issue from sveltejs/svelte Nov 1, 2024
@dummdidumm dummdidumm added the bug Something isn't working label Nov 5, 2024
dummdidumm added a commit that referenced this issue Nov 5, 2024
So far we've always assumed that a Svelte file is a TS file for simplicity, due to our default language (which was removed a long time ago) and also because IIRC there were issues with TS having its snapshot scriptKind being switched. That seems to be no longer the case, and so we can get better at properly analyzing whether or not this is a TS file, to allow JSDoc to take action when it's a JS file.

#2555
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants