Skip to content

Commit ab08ed6

Browse files
committed
feat: don't use deprecated SvelteComponentTyped import if shims path hints at v4
1 parent 25a531f commit ab08ed6

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

packages/svelte2tsx/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ export interface EmitDtsConfig {
8686
/**
8787
* Path to `svelte-shims.d.ts` of `svelte2tsx`.
8888
* Example: `require.resolve('svelte2tsx/svelte-shims.d.ts')`
89+
*
90+
* If a path is given that points to `svelte-shims-v4.d.ts`,
91+
* the `SvelteComponent` import is used instead of
92+
* `SvelteComponentTyped` which is deprecated in Svelte v4.
8993
*/
9094
svelteShimsPath: string;
9195
/**

packages/svelte2tsx/src/svelte2tsx/addComponentExport.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface AddComponentExportPara {
2020
componentDocumentation: ComponentDocumentation;
2121
mode: 'ts' | 'dts' | 'tsx';
2222
generics: Generics;
23+
noSvelteComponentTyped?: boolean;
2324
}
2425

2526
/**
@@ -45,7 +46,8 @@ function addGenericsComponentExport({
4546
mode,
4647
usesAccessors,
4748
str,
48-
generics
49+
generics,
50+
noSvelteComponentTyped
4951
}: AddComponentExportPara) {
5052
const genericsDef = generics.toDefinitionString();
5153
const genericsRef = generics.toReferencesString();
@@ -71,20 +73,24 @@ class __sveltets_Render${genericsDef} {
7173
}
7274
`;
7375

76+
const svelteComponentClass = noSvelteComponentTyped
77+
? 'SvelteComponent'
78+
: 'SvelteComponentTyped';
79+
7480
if (mode === 'dts') {
7581
statement +=
7682
`export type ${className}Props${genericsDef} = ${returnType('props')};\n` +
7783
`export type ${className}Events${genericsDef} = ${returnType('events')};\n` +
7884
`export type ${className}Slots${genericsDef} = ${returnType('slots')};\n` +
7985
`\n${doc}export default class${
8086
className ? ` ${className}` : ''
81-
}${genericsDef} extends SvelteComponentTyped<${className}Props${genericsRef}, ${className}Events${genericsRef}, ${className}Slots${genericsRef}> {` +
87+
}${genericsDef} extends ${svelteComponentClass}<${className}Props${genericsRef}, ${className}Events${genericsRef}, ${className}Slots${genericsRef}> {` +
8288
exportedNames.createClassGetters() +
8389
(usesAccessors ? exportedNames.createClassAccessors() : '') +
8490
'\n}';
8591
} else {
8692
statement +=
87-
'\n\nimport { SvelteComponentTyped as __SvelteComponentTyped__ } from "svelte" \n' +
93+
`\n\nimport { ${svelteComponentClass} as __SvelteComponentTyped__ } from "svelte" \n` +
8894
`${doc}export default class${
8995
className ? ` ${className}` : ''
9096
}${genericsDef} extends __SvelteComponentTyped__<${returnType('props')}, ${returnType(
@@ -107,7 +113,8 @@ function addSimpleComponentExport({
107113
fileName,
108114
mode,
109115
usesAccessors,
110-
str
116+
str,
117+
noSvelteComponentTyped
111118
}: AddComponentExportPara) {
112119
const propDef = props(
113120
isTsFile,
@@ -135,14 +142,18 @@ function addSimpleComponentExport({
135142
return `type ${replacement} = typeof __propDef.${type.toLowerCase()};\nexport { ${replacement} as ${exportName} };\n`;
136143
};
137144

145+
const svelteComponentClass = noSvelteComponentTyped
146+
? 'SvelteComponent'
147+
: 'SvelteComponentTyped';
148+
138149
statement =
139150
`\nconst __propDef = ${propDef};\n` +
140151
addTypeExport('Props') +
141152
addTypeExport('Events') +
142153
addTypeExport('Slots') +
143154
`\n${doc}export default class${
144155
className ? ` ${className}` : ''
145-
} extends SvelteComponentTyped<${className}Props, ${className}Events, ${className}Slots> {` +
156+
} extends ${svelteComponentClass}<${className}Props, ${className}Events, ${className}Slots> {` +
146157
exportedNames.createClassGetters() +
147158
(usesAccessors ? exportedNames.createClassAccessors() : '') +
148159
'\n}';

packages/svelte2tsx/src/svelte2tsx/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ export function svelte2tsx(
311311
mode?: 'ts' | 'dts';
312312
accessors?: boolean;
313313
typingsNamespace?: string;
314+
noSvelteComponentTyped?: boolean;
314315
} = {}
315316
) {
316317
options.mode = options.mode || 'ts';
@@ -418,15 +419,20 @@ export function svelte2tsx(
418419
fileName: options?.filename,
419420
componentDocumentation,
420421
mode: options.mode,
421-
generics
422+
generics,
423+
noSvelteComponentTyped: options.noSvelteComponentTyped
422424
});
423425

424426
if (options.mode === 'dts') {
425427
// Prepend the import which is used for TS files
426428
// The other shims need to be provided by the user ambient-style,
427429
// for example through filenames.push(require.resolve('svelte2tsx/svelte-shims.d.ts'))
428430
// TODO replace with SvelteComponent for Svelte 5, keep old for backwards compatibility with Svelte 3
429-
str.prepend('import { SvelteComponentTyped } from "svelte"\n' + '\n');
431+
if (options.noSvelteComponentTyped) {
432+
str.prepend('import { SvelteComponent } from "svelte"\n' + '\n');
433+
} else {
434+
str.prepend('import { SvelteComponentTyped } from "svelte"\n' + '\n');
435+
}
430436
let code = str.toString();
431437
// Remove all tsx occurences and the template part from the output
432438
code = code

0 commit comments

Comments
 (0)