Last updated
TypeScript to JavaScript Converter Examples
The TypeScript to JavaScript Converter removes type annotations and TypeScript-specific syntax, producing plain JavaScript. Below are practical examples showing what each TypeScript construct compiles to.
Type Annotations Removed
// TypeScript
const name: string = "Alice";
const age: number = 30;
const active: boolean = true;
let items: string[] = [];
let count: number | null = null;
// JavaScript (types removed)
const name = "Alice";
const age = 30;
const active = true;
let items = [];
let count = null;
Function Type Annotations
// TypeScript
function greet(name: string, greeting: string = "Hello"): string {
return `${greeting}, ${name}!`;
}
async function fetchUser(id: number): Promise<User> {
const response = await fetch(`/api/users/${id}`);
return response.json();
}
// JavaScript (types removed)
function greet(name, greeting = "Hello") {
return `${greeting}, ${name}!`;
}
async function fetchUser(id) {
const response = await fetch(`/api/users/${id}`);
return response.json();
}
Interface Removal
// TypeScript
interface User {
id: number;
name: string;
email: string;
role: 'admin' | 'user';
createdAt: Date;
}
interface ApiResponse<T> {
data: T;
status: number;
message: string;
}
function createUser(user: User): ApiResponse<User> {
return { data: user, status: 201, message: 'Created' };
}
// JavaScript (interfaces completely removed)
function createUser(user) {
return { data: user, status: 201, message: 'Created' };
}
Type Alias Removal
// TypeScript
type UserId = number;
type Status = 'active' | 'inactive' | 'pending';
type Callback<T> = (error: Error | null, result: T) => void;
function getUser(id: UserId): Status {
return 'active';
}
// JavaScript (type aliases removed)
function getUser(id) {
return 'active';
}
Generic Functions
// TypeScript
function identity<T>(value: T): T {
return value;
}
function first<T>(arr: T[]): T | undefined {
return arr[0];
}
function merge<T, U>(obj1: T, obj2: U): T & U {
return { ...obj1, ...obj2 };
}
// JavaScript (generic type parameters removed)
function identity(value) {
return value;
}
function first(arr) {
return arr[0];
}
function merge(obj1, obj2) {
return { ...obj1, ...obj2 };
}
Class with Access Modifiers
// TypeScript
class UserService {
private readonly db: Database;
protected logger: Logger;
public name: string;
constructor(db: Database, logger: Logger) {
this.db = db;
this.logger = logger;
this.name = 'UserService';
}
public async findById(id: number): Promise<User | null> {
return this.db.query<User>(`SELECT * FROM users WHERE id = ?`, [id]);
}
private validateId(id: number): boolean {
return id > 0;
}
}
// JavaScript (access modifiers removed)
class UserService {
constructor(db, logger) {
this.db = db;
this.logger = logger;
this.name = 'UserService';
}
async findById(id) {
return this.db.query(`SELECT * FROM users WHERE id = ?`, [id]);
}
validateId(id) {
return id > 0;
}
}
Enum Conversion
// TypeScript
enum Direction {
Up = 'UP',
Down = 'DOWN',
Left = 'LEFT',
Right = 'RIGHT'
}
enum Status {
Active = 1,
Inactive = 2,
Pending = 3
}
// JavaScript (enums → objects)
const Direction = {
Up: 'UP',
Down: 'DOWN',
Left: 'LEFT',
Right: 'RIGHT'
};
const Status = {
Active: 1,
Inactive: 2,
Pending: 3
};
Type Assertions
// TypeScript
const input = document.getElementById('name') as HTMLInputElement;
const value = (someValue as string).toUpperCase();
const num = <number>someValue;
// JavaScript (type assertions removed)
const input = document.getElementById('name');
const value = someValue.toUpperCase();
const num = someValue;
Optional Chaining and Nullish Coalescing
// TypeScript (ES2020+ features — preserved in modern JS output)
const city = user?.address?.city;
const name = user?.name ?? 'Anonymous';
const length = arr?.length ?? 0;
// JavaScript (ES2020+ — preserved as-is)
const city = user?.address?.city;
const name = user?.name ?? 'Anonymous';
const length = arr?.length ?? 0;
// JavaScript (ES5 target — compiled to older syntax)
const city = user === null || user === void 0
? void 0
: (_a = user.address) === null || _a === void 0
? void 0
: _a.city;
const name = (user === null || user === void 0 ? void 0 : user.name) !== null
&& (user === null || user === void 0 ? void 0 : user.name) !== void 0
? user.name : 'Anonymous';
Decorators
// TypeScript (with decorators)
@Injectable()
@Controller('/users')
class UserController {
@Get('/:id')
async getUser(@Param('id') id: string): Promise<User> {
return this.userService.findById(parseInt(id));
}
}
// JavaScript (decorators compiled)
let UserController = class UserController {
async getUser(id) {
return this.userService.findById(parseInt(id));
}
};
UserController = __decorate([
Injectable(),
Controller('/users')
], UserController);
__decorate([
Get('/:id'),
__param(0, Param('id'))
], UserController.prototype, "getUser", null);
React Component (TSX → JSX)
// TypeScript (TSX)
interface ButtonProps {
label: string;
onClick: () => void;
disabled?: boolean;
variant?: 'primary' | 'secondary';
}
const Button: React.FC<ButtonProps> = ({
label,
onClick,
disabled = false,
variant = 'primary'
}) => {
return (
<button
className={`btn btn-${variant}`}
onClick={onClick}
disabled={disabled}
>
{label}
</button>
);
};
// JavaScript (JSX — types removed)
const Button = ({
label,
onClick,
disabled = false,
variant = 'primary'
}) => {
return (
<button
className={`btn btn-${variant}`}
onClick={onClick}
disabled={disabled}
>
{label}
</button>
);
};
Common Use Cases
- Understand what JavaScript your TypeScript compiles to
- Debug type-related issues by seeing the runtime code
- Migrate TypeScript code to JavaScript environments
- Learn TypeScript by comparing TS and JS side by side
- Verify that TypeScript features compile correctly
- Prepare TypeScript snippets for JavaScript-only environments
Paste your TypeScript code into the converter and instantly see the equivalent JavaScript output.