Skip to content

Files

Latest commit

74b6caf · Mar 27, 2025

History

History

svelte2tsx

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Feb 12, 2025
Mar 19, 2025
Mar 19, 2025
Jul 25, 2020
Mar 30, 2021
Mar 28, 2021
May 21, 2020
Dec 22, 2022
Feb 12, 2025
Mar 27, 2025
Mar 16, 2023
Jul 29, 2023
Sep 18, 2024
Dec 21, 2022
Nov 22, 2024
Sep 18, 2024
Jan 27, 2022

svelte2tsx

Converts Svelte component source into TSX. The TSX can be type checked using the included svelte-jsx.d.ts and svelte-shims.d.ts.

This project only converts svelte to tsx, type checking is left to consumers of this plugin such as language services

type SvelteCompiledToTsx = {
    code: string;
    map: import('magic-string').SourceMap;
};

export default function svelte2tsx(svelte: string): SvelteCompiledToTsx;

For example

Input.svelte

<script>
    export let world = 'name';
</script>

<h1>hello {world}</h1>

will produce this ugly but type checkable TSX

<></>;
function render() {
    let world = 'name';
    <>
        <h1>hello {world}</h1>
    </>;
    return { props: { world }, slots: {}, events: {} };
}

export default class _World_ extends __sveltets_2_createSvelte2TsxComponent(
    __sveltets_2_partial(__sveltets_2_with_any_event(render))
) {}

with a v3 SourceMap back to the original source.

For more examples of the transformations, see the test/**/samples folders

Credits

  • halfnelson for creating svelte2tsx
  • pushkine for creating the source mapping test infrastructure