SvelteKit `dist/index.d.ts` 导出组件而不是类型。

huangapple go评论53阅读模式
英文:

SvelteKit `dist/index.d.ts` exports component instead of types

问题

我有一个SvelteKit(v1.5)vite库项目,它从 dist/index.js 导出一个单一组件:

export { default as Settings } from './components/Settings.svelte';

我还在 src/types/index.d.ts 中有一个简单的类型定义,我想为库用户导出它:

// ...
export type SettingsWritable = ...

我也有一个简单的类型定义在 src/types/index.d.ts 中,我想为库用户导出它:

// ...
export type SettingsWritable = ...

我 - 也许天真地 - 期望这个被 dist/index.d.ts 导出。然而 dist/index.d.ts 的内容与 dist/index.js 相同 - 如上导出组件。

我的期望是错误的吗?如果不是,我该如何确保我的类型定义(目前在 src/types/index.d.ts 中)被正确导出?

这里有一些可能有帮助的 package.json 属性:

"exports": {
    ".": {
        "types": "./dist/index.d.ts",
        "svelte": "./dist/index.js"
    }
},
"files": [
    "dist",
    "!dist/**/*.test.*",
    "!dist/**/*.spec.*",
    "build",
    "riga-tool.config.json"
],
"main": "./dist/index.js",
"svelte": "./dist/index.js",
"types": "./dist/index.d.ts",
"type": "module"

我也使用了SvelteKit starter仓库中的标准 tsconfig:

{
	"compilerOptions": {
		// ...
		"rootDirs": [
			"..",
			"./types"
		],
		// ...
	},
	"include": [
		"ambient.d.ts",
		"./types/**/$types.d.ts",
		"../vite.config.ts",
		"../src/**/*.js",
		"../src/**/*.ts",
		"../src/**/*.svelte",
		"../tests/**/*.js",
		"../tests/**/*.ts",
		"../tests/**/*.svelte"
	],
	"exclude": [
		"../node_modules/**",
		"./[!ambient.d.ts]**",
		"../src/service-worker.js",
		"../src/service-worker.ts",
		"../src/service-worker.d.ts"
	]
}

非常感谢任何指点!

英文:

I have a SvelteKit (v1.5) vite library project that exports a single component from dist/index.js:

export { default as Settings } from './components/Settings.svelte';

I also have one simple type definition in src/types/index.d.ts I would like to export for lib users:

//...
export type SettingsWritable = ...

I - maybe naively - would expect this to be exported by dist/index.d.ts. However dist/index.d.ts has the same content as dist/index.js - exporting the component as above.

Is my expectation wrong? If not, how would I make sure my type definitions (currently in src/types/index.d.ts) are exported correctly?

Here are some possibly helpful package.json properties:

"exports": {
    ".": {
        "types": "./dist/index.d.ts",
        "svelte": "./dist/index.js"
    }
},
"files": [
    "dist",
    "!dist/**/*.test.*",
    "!dist/**/*.spec.*",
    "build",
    "riga-tool.config.json"
],
"main": "./dist/index.js",
"svelte": "./dist/index.js",
"types": "./dist/index.d.ts",
"type": "module"

I'm also using the standard tsconfig from the SvelteKit starter repo:

{
	"compilerOptions": {
		// ...
		"rootDirs": [
			"..",
			"./types"
		],
		// ...
	},
	"include": [
		"ambient.d.ts",
		"./types/**/$types.d.ts",
		"../vite.config.ts",
		"../src/**/*.js",
		"../src/**/*.ts",
		"../src/**/*.svelte",
		"../tests/**/*.js",
		"../tests/**/*.ts",
		"../tests/**/*.svelte"
	],
	"exclude": [
		"../node_modules/**",
		"./[!ambient.d.ts]**",
		"../src/service-worker.js",
		"../src/service-worker.ts",
		"../src/service-worker.d.ts"
	]
}

Many thanks for any pointers!

答案1

得分: 2

组件的类型是自动生成的,应该提供为 [name].svelte.d.ts,如果需要暴露额外的类型,只需与组件一起导出它们。例如:

export { default as Settings } from './components/Settings.svelte';
export * from './types'; // 如果有 index.ts

建议对于“新”类型使用 .ts,仅在类型声明用于现有的 .js/其他文件时使用 .d.ts

英文:

The types for components are generated automatically and should be provided as [name].svelte.d.ts, if you need to expose additional types, you can just export them alongside the components. E.g.

export { default as Settings } from './components/Settings.svelte';
export * from './types'; // if there is an index.ts

Would recommend using .ts for "new" types and .d.ts only if the type declarations are for existing .js/other files.

huangapple
  • 本文由 发表于 2023年6月13日 15:58:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定