DEV Community

Cover image for How does VuReact handle Vue 3's top-level TypeScript declarations in React?
Ryan John
Ryan John

Posted on • Originally published at vureact.top

How does VuReact handle Vue 3's top-level TypeScript declarations in React?

VuReact is a compiler toolchain for migrating from Vue to React — and for writing React with Vue 3 syntax.

In this article, we will look at how VuReact handles top-level TypeScript declarations when compiling Vue 3 code to React.

Before We Start

To keep the examples easy to read, this article follows two simple conventions:

  1. All Vue and React snippets focus on core logic only, with full component wrappers and unrelated configuration omitted.
  2. The discussion assumes you are already familiar with TypeScript.

Compilation Mapping

Top-level TypeScript declarations -> hoisted outside the React component

In Vue, top-level TypeScript declarations inside <script setup> such as interface, type, or enum are purely static language constructs. They do not produce runtime behavior.

VuReact performs static analysis on those declarations and hoists them outside the generated React component so they remain at module scope, preserve their intended visibility, and do not interfere with runtime logic.

  • Vue
<script setup lang="ts">
export interface ExampleInterface { /* ... */ }

enum ExampleEnum { /* ... */ }

function func() {
  type ExampleType = { /* ... */ };
}
</script>
Enter fullscreen mode Exit fullscreen mode
  • Compiled React
export interface ExampleInterface { /* ... */ }

enum ExampleEnum { /* ... */ }

const Example = memo(() => {
  function func() {
    type ExampleType = { /* ... */ };
  }

  return <></>;
});
Enter fullscreen mode Exit fullscreen mode

As the example shows, top-level type declarations are preserved outside the component as module-level TypeScript declarations, while inner type declarations remain inside their original function scope.

That allows VuReact to preserve TypeScript semantics without introducing unnecessary changes to the runtime structure of the compiled React component.

Related Links

Top comments (0)