Vite TypeScript
Vite supports TypeScript out of the box. You can write .ts and .tsx files, reference them from index.html, and import them from each other — no plugin or loader needed.
Transpile only, no type-checking
The single most important fact about TypeScript in Vite:
Vite removes types; it does not check them.
Each .ts file is transformed in isolation by a very fast native compiler (Oxc in Vite 8; esbuild in Vite 5–7). It deletes type annotations and turns the file into JavaScript in microseconds. Type errors do not stop the dev server or the build.
Type-checking is the job of the TypeScript compiler, tsc, run separately. That's why the -ts templates use:
{
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"typecheck": "tsc --noEmit"
}
}Let's see the difference. Suppose src/main.ts contains a type error:
const total: number = 'ten'; // wrong type
console.log(total);npx vite buildvite v8.3.4 building client environment for production...
✓ 4 modules transformed.
dist/index.html 0.46 kB │ gzip: 0.30 kB
dist/assets/index-B2nQx8Pa.js 0.71 kB │ gzip: 0.41 kB
✓ built in 58ms
npx tsc --noEmitsrc/main.ts:1:7 - error TS2322: Type 'string' is not assignable to type 'number'.
1 const total: number = 'ten'; // wrong type
~~~~~
Found 1 error in src/main.ts:1
Vite happily built it; tsc caught it. Your editor (VS Code uses the same TypeScript language service) also shows the error while you type.
Tip: Want errors in the browser overlay too? The community plugin
vite-plugin-checkerrunstscin a worker during dev.
What type stripping looks like
Conceptually, transpiling TypeScript mostly means erasing annotations. This toy version removes simple : type annotations and interface blocks — real tools use a full parser, of course:
function stripTypes(ts) {
return ts
.replace(/^\s*(export\s+)?interface\s+\w+\s*\{[^}]*\}\s*$/gm, '')
.replace(/(\w)\s*:\s*[A-Za-z_][\w<>\[\]|]*(?=\s*[=,)])/g, '$1')
.replace(/\)\s*:\s*[A-Za-z_][\w<>\[\]| ]*\s*\{/g, ') {')
.replace(/\n{2,}/g, '\n')
.trim();
}
const source = `
interface User { name: string; age: number }
function greet(user: User, loud: boolean): string {
const text: string = 'Hi ' + user.name;
return loud ? text.toUpperCase() : text;
}`;
console.log(stripTypes(source));function greet(user, loud) {
const text = 'Hi ' + user.name;
return loud ? text.toUpperCase() : text;
}
isolatedModules and type-only imports
Because each file is compiled alone, the compiler cannot look into other files to learn whether an import is a type or a value. Help it with import type:
import type { User } from './types'; // erased completely
import { type Role, createUser } from './users'; // Role erased, createUser keptTurn on these tsconfig.json options so tsc warns you about patterns that single-file compilers cannot handle:
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "bundler",
"isolatedModules": true,
"verbatimModuleSyntax": true,
"allowImportingTsExtensions": true,
"noEmit": true,
"strict": true,
"types": ["vite/client"]
},
"include": ["src"]
}| Option | Why it matters with Vite |
|---|---|
moduleResolution: "bundler" |
Matches how Vite resolves imports (package exports, no extensions needed) |
isolatedModules |
Errors on code that can't be compiled file-by-file (e.g. re-exporting a type without type) |
verbatimModuleSyntax |
Keeps imports exactly as written except type ones — predictable output |
noEmit |
tsc only checks; Vite produces the JavaScript |
types: ["vite/client"] |
Types for import.meta.env, import.meta.hot, asset imports |
Enums, decorators and other emitted syntax
Most TypeScript is erasable, but a few features generate runtime code: enum, namespace, parameter properties (constructor(private x)) and legacy decorators. Vite's compiler supports them, yet they are good to understand — this is roughly what a numeric enum Color { Red, Green } compiles to:
var Color;
(function (Color) {
Color[Color['Red'] = 0] = 'Red';
Color[Color['Green'] = 1] = 'Green';
})(Color || (Color = {}));
console.log(Color.Green);
console.log(Color[0]);
console.log(Color);1
Red
{"0":"Red","1":"Green","Red":0,"Green":1}
Note: Many teams prefer
as constobjects or union types (type Color = 'red' | 'green') over enums: they are fully erasable and produce smaller code.
Typing import.meta.env
Add your own environment variables to the ImportMetaEnv interface in src/vite-env.d.ts:
/// <reference types="vite/client" />
interface ImportMetaEnv {
readonly VITE_API_URL: string;
}
interface ImportMeta {
readonly env: ImportMetaEnv;
}Config files in TypeScript
vite.config.ts works out of the box; Vite bundles the config with its own tooling before running it. Use defineConfig for autocompletion:
import { defineConfig } from 'vite';
export default defineConfig({
build: { target: 'es2022' },
});Test yourself
Does vite build fail when your TypeScript has type errors?
- Yes, always
- No, Vite only strips types; run
tsc --noEmitto check them - Only in strict mode
- Only for .tsx files
Why should you write import type { User } from './types'?
- It is faster at runtime
- So single-file compilers know the import can be erased
- It is required for JSON
- It enables HMR
Which tsconfig option provides types for import.meta.env and asset imports?
- "lib": ["dom"]
- "types": ["vite/client"]
- "strict": true
- "jsx": "react"