英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论