java-to-ts-ts
purpose
Rewriting Java source code as idiomatic TypeScript — mapping Java's class-based OOP, generics, annotations, collections, and concurrency patterns to their TypeScript equivalents.
rules
- Java class hierarchies map to TypeScript interfaces + classes. Prefer interfaces over abstract classes for defining contracts. Java
implements Interfacemaps directly to TypeScriptimplements Interface. Javaextends AbstractClassmaps to TypeScriptextends BaseClass. - Java generics map to TypeScript generics with the same
<T>syntax. Key difference: Java generics are erased at runtime; TypeScript generics are erased at compile time. Both are structural at their core. Java bounded wildcards (? extends T) map to TS constrained generics (<U extends T>). Java? super Thas no direct TS equivalent — use a union or contravariant generic. - Java annotations (
@Override,@Deprecated,@JsonProperty) have no built-in TS equivalent. Map to: TS decorators (experimental, stage 3), JSDoc comments, or runtime metadata patterns. For simple markers like@Override, simply remove them — TS enforces override correctness with theoverridekeyword. - Java
Optional<T>maps toT | nullorT | undefined.Optional.of(x)→ justx,Optional.empty()→null,Optional.isPresent()→!= null,Optional.map(fn)→ optional chaining + nullish coalescing (x?.transform() ?? default). - Java checked exceptions do not exist in TypeScript. Remove
throwsdeclarations from method signatures. Converttry/catchblocks but let unexpected errors propagate naturally. Document thrown errors in JSDoc if important for callers. - Java
finalmaps toreadonlyfor class fields andconstfor local variables. Javafinalon method parameters has no TS equivalent (parameters are already effectively final by convention). - Java
staticmethods and fields map directly to TypeScriptstatic. Java static utility classes (e.g.,Collections,Math) often map better to standalone exported functions rather than a class with all-static members. - Java
enummaps to TypeScriptenumfor simple cases, but prefer string literal unions for most use cases. Java enums with methods and fields → TypeScriptas constobject + associated functions or a class hierarchy. - Java
StreamAPI maps to TypeScript array methods.stream().filter().map().collect(Collectors.toList())becomes.filter().map().Collectors.toMap()→reduce()orObject.fromEntries().Collectors.groupingBy()→Object.groupBy()orreduce(). - Java
synchronized/volatile/Lockhave no TypeScript equivalent (JS is single-threaded). Remove synchronization primitives entirely. If the Java code uses threads for parallelism, redesign aroundPromise.all(), async/await, or worker threads. - Java
Map<K,V>maps toMap<K,V>(JS built-in) orRecord<string, V>for string-keyed maps.List<T>→T[]orArray<T>.Set<T>→Set<T>. JavaHashMap/TreeMapdistinctions are irrelevant — JSMaphas insertion-order iteration. - Java getter/setter pairs (
getName()/setName()) should be simplified to direct property access in TS. Only useget/setaccessors if validation or side effects are needed. - Java
StringBuilder/ string concatenation in loops → template literals orArray.join(). TS strings are immutable like Java strings, but template literals handle most interpolation needs. - Java package structure (
com.example.app.service) does NOT map to deeply nested TS folders. Flatten to a pragmatic folder structure:src/services/,src/models/, etc. Use barrel files (index.ts) for clean re-exports. - Lombok
@Datagenerates getters, setters,equals(),hashCode(),toString(), and a required-args constructor. In TypeScript, replace with a plaininterface(for data-only types) or aclasswithpublicconstructor parameters. Remove all generated method equivalents — TS doesn't need them. - Lombok
@Buildergenerates a fluent builder class. Replace with a TypeScript options interface:new Foo({ bar, baz })or a factory function. The builder pattern is unnecessary when constructors accept named parameters via object destructuring. - Lombok
@Getter/@Setteron individual fields → directpublicproperty access in TS. If the field was@Getteronly (read-only), usereadonly. If@Setterhas custom logic, use a TSsetaccessor. - Lombok
@AllArgsConstructor/@NoArgsConstructor/@RequiredArgsConstructor→ TypeScript constructor with explicit parameters.@NoArgsConstructoron a data class → all properties optional or have defaults.@RequiredArgsConstructor→ constructor with onlyfinalfields as parameters. - Lombok
@Slf4jgenerates aprivate static final Logger logfield. Replace with a module-level logger:import pino from 'pino'; const log = pino({ name: 'MyClass' });or accept a logger via constructor injection. - Lombok
@Value(immutable@Data) → TypeScriptinterfacewith allreadonlyfields, or useReadonly<T>utility type. CompletableFuture<T>maps toPromise<T>.thenApply(fn)→.then(fn),thenCompose(fn)→.then(fn)(Promise auto-flattens),exceptionally(fn)→.catch(fn),thenAccept(fn)→.then(fn)(when return is void).CompletableFuture.allOf()→Promise.all().CompletableFuture.anyOf()→Promise.race().CompletableFuture.supplyAsync(fn, executor)→ just call the async function directly (no executor needed in single-threaded JS).CompletableFuturechains should be rewritten asasync/awaitfor readability. A chain of.thenApply().thenCompose().exceptionally()becomes a simpletry { const a = await step1(); const b = await step2(a); } catch (e) { ... }.@FunctionalInterfaceannotations → TypeScript function type aliases.@FunctionalInterface interface Handler<T> { void handle(T t); }becomestype Handler<T> = (t: T) => void.
patterns
Java class hierarchy → TypeScript interfaces + classes
// --- Before (Java) ---
public interface MessageHandler {
void handle(Message message);
boolean canHandle(String type);
}
public abstract class BaseHandler implements MessageHandler {
protected final Logger logger;
public BaseHandler(Logger logger) {
this.logger = logger;
}
@Override
public boolean canHandle(String type) {
return getSupportedTypes().contains(type);
}
protected abstract Set<String> getSupportedTypes();
}
public class SlashCommandHandler extends BaseHandler {
private final CommandRegistry registry;
public SlashCommandHandler(Logger logger, CommandRegistry registry) {
super(logger);
this.registry = registry;
}
@Override
public void handle(Message message) {
String command = message.getText().split(" ")[0];
registry.execute(command, message);
}
@Override
protected Set<String> getSupportedTypes() {
return Set.of("slash_command", "block_actions");
}
}// --- After (TypeScript) ---
interface MessageHandler {
handle(message: Message): void;
canHandle(type: string): boolean;
}
abstract class BaseHandler implements MessageHandler {
constructor(protected readonly logger: Logger) {}
canHandle(type: string): boolean {
return this.getSupportedTypes().has(type);
}
abstract handle(message: Message): void;
protected abstract getSupportedTypes(): Set<string>;
}
class SlashCommandHandler extends BaseHandler {
constructor(
logger: Logger,
private readonly registry: CommandRegistry,
) {
super(logger);
}
handle(message: Message): void {
const command = message.text.split(' ')[0];
this.registry.execute(command, message);
}
protected getSupportedTypes(): Set<string> {
return new Set(['slash_command', 'block_actions']);
}
}Java Stream API → TypeScript array methods
// --- Before (Java) ---
import java.util.stream.Collectors;
List<UserDTO> activeUsers = users.stream()
.filter(u -> u.isActive())
.filter(u -> !u.getRole().equals(Role.GUEST))
.sorted(Comparator.comparing(User::getName))
.map(u -> new UserDTO(u.getName(), u.getEmail()))
.collect(Collectors.toList());
Map<String, List<User>> byDepartment = users.stream()
.collect(Collectors.groupingBy(User::getDepartment));
Optional<User> admin = users.stream()
.filter(u -> u.getRole().equals(Role.ADMIN))
.findFirst();// --- After (TypeScript) ---
interface UserDTO {
name: string;
email: string;
}
const activeUsers: UserDTO[] = users
.filter((u) => u.active)
.filter((u) => u.role !== 'guest')
.sort((a, b) => a.name.localeCompare(b.name))
.map((u) => ({ name: u.name, email: u.email }));
const byDepartment: Record<string, User[]> = Object.groupBy(
users,
(u) => u.department,
) as Record<string, User[]>;
const admin: User | undefined = users.find((u) => u.role === 'admin');Java enum with behavior → TypeScript const object + functions
// --- Before (Java) ---
public enum Priority {
HIGH(1, "High Priority"),
MEDIUM(2, "Medium Priority"),
LOW(3, "Low Priority");
private final int level;
private final String label;
Priority(int level, String label) {
this.level = level;
this.label = label;
}
public int getLevel() { return level; }
public String getLabel() { return label; }
public boolean isUrgent() {
return this == HIGH;
}
}// --- After (TypeScript) ---
const Priority = {
HIGH: { level: 1, label: 'High Priority' },
MEDIUM: { level: 2, label: 'Medium Priority' },
LOW: { level: 3, label: 'Low Priority' },
} as const;
type PriorityKey = keyof typeof Priority;
type PriorityValue = (typeof Priority)[PriorityKey];
function isUrgent(priority: PriorityValue): boolean {
return priority === Priority.HIGH;
}Lombok @Data/@Builder → TypeScript interface + options constructor
// --- Before (Java with Lombok) ---
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Data
@Builder
@Slf4j
public class SlackMessage {
private final String channel;
private final String text;
private final String threadTs;
private final boolean unfurlLinks;
private final List<Attachment> attachments;
public void send(WebClient client) {
log.info("Sending message to {}", channel);
client.chatPostMessage(r -> r
.channel(channel)
.text(text)
.threadTs(threadTs)
.unfurlLinks(unfurlLinks)
.attachments(attachments));
}
}
// Usage with builder:
SlackMessage msg = SlackMessage.builder()
.channel("#general")
.text("Hello!")
.unfurlLinks(false)
.build();
msg.send(client);// --- After (TypeScript) ---
import pino from 'pino';
const log = pino({ name: 'SlackMessage' });
interface SlackMessageOptions {
channel: string;
text: string;
threadTs?: string;
unfurlLinks?: boolean;
attachments?: Attachment[];
}
// Interface replaces @Data — no getters/setters/equals/hashCode/toString needed
// Options object replaces @Builder — named params via destructuring
class SlackMessage {
readonly channel: string;
readonly text: string;
readonly threadTs?: string;
readonly unfurlLinks: boolean;
readonly attachments: Attachment[];
constructor({
channel,
text,
threadTs,
unfurlLinks = false,
attachments = [],
}: SlackMessageOptions) {
this.channel = channel;
this.text = text;
this.threadTs = threadTs;
this.unfurlLinks = unfurlLinks;
this.attachments = attachments;
}
send(client: WebClient): void {
log.info(`Sending message to ${this.channel}`);
client.chat.postMessage({
channel: this.channel,
text: this.text,
thread_ts: this.threadTs,
unfurl_links: this.unfurlLinks,
attachments: this.attachments,
});
}
}
// Usage — options object replaces builder chain:
const msg = new SlackMessage({
channel: '#general',
text: 'Hello!',
unfurlLinks: false,
});
msg.send(client);CompletableFuture chain → async/await
// --- Before (Java) ---
import java.util.concurrent.CompletableFuture;
public class AsyncSlackClient {
private final MethodsClient client;
private final ExecutorService executor;
public CompletableFuture<String> fetchAndNotify(String userId, String channel) {
return CompletableFuture.supplyAsync(() -> client.usersInfo(r -> r.user(userId)), executor)
.thenApply(response -> response.getUser().getRealName())
.thenCompose(name -> CompletableFuture.supplyAsync(
() -> client.chatPostMessage(r -> r.channel(channel).text("Hello " + name)),
executor
))
.thenApply(response -> response.getTs())
.exceptionally(ex -> {
log.error("Failed: {}", ex.getMessage());
return null;
});
}
public CompletableFuture<List<String>> fetchMultipleUsers(List<String> userIds) {
List<CompletableFuture<String>> futures = userIds.stream()
.map(id -> CompletableFuture.supplyAsync(
() -> client.usersInfo(r -> r.user(id)).getUser().getRealName(),
executor
))
.collect(Collectors.toList());
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
.thenApply(v -> futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList()));
}
}// --- After (TypeScript) ---
class AsyncSlackClient {
constructor(private readonly client: WebClient) {}
// CompletableFuture chain → simple async/await
async fetchAndNotify(userId: string, channel: string): Promise<string | null> {
try {
const userResponse = await this.client.users.info({ user: userId });
const name = userResponse.user?.real_name ?? 'Unknown';
const msgResponse = await this.client.chat.postMessage({
channel,
text: `Hello ${name}`,
});
return msgResponse.ts ?? null;
} catch (err) {
log.error(`Failed: ${(err as Error).message}`);
return null;
}
}
// CompletableFuture.allOf → Promise.all
async fetchMultipleUsers(userIds: string[]): Promise<string[]> {
const results = await Promise.all(
userIds.map(async (id) => {
const response = await this.client.users.info({ user: id });
return response.user?.real_name ?? 'Unknown';
}),
);
return results;
}
}@FunctionalInterface → TypeScript function types
// --- Before (Java) ---
@FunctionalInterface
public interface BoltEventHandler<E extends Event> {
Response apply(EventsApiPayload<E> payload, EventContext context) throws Exception;
}
@FunctionalInterface
public interface Middleware {
Response apply(Request req, Response resp, MiddlewareChain chain) throws Exception;
}
// Usage:
app.event(AppMentionEvent.class, (payload, ctx) -> {
ctx.say("Hello!");
return ctx.ack();
});// --- After (TypeScript) ---
// @FunctionalInterface → type alias for the function signature
type BoltEventHandler<E extends Event> = (
payload: EventsApiPayload<E>,
context: EventContext,
) => Promise<Response>;
type Middleware = (
req: Request,
resp: Response,
chain: MiddlewareChain,
) => Promise<Response>;
// Usage — identical lambda syntax:
app.event(AppMentionEvent, async (payload, ctx) => {
await ctx.say('Hello!');
return ctx.ack();
});pitfalls
- Null vs undefined: Java has one null; TypeScript has
nullANDundefined. Decide on a convention early. Recommendation: useundefinedfor "not provided" (optional params),nullfor "explicitly empty" (API responses). - No method overloading at runtime: Java allows multiple methods with the same name but different signatures. TypeScript supports overload signatures but only one implementation. Merge overloads into a single function with union parameter types.
- Access modifiers are compile-time only: TypeScript's
private/protectedare erased at runtime (unlike Java). For true runtime privacy, use#privateField(ES2022 private fields). - No runtime type checking: Java's
instanceofchecks actual class identity. TypeScript'sinstanceofworks for classes but NOT for interfaces (they're erased). Use discriminated unions or type guard functions instead. - Collections are not auto-imported: Java's
List,Map,Setare imports fromjava.util. TypeScript'sArray,Map,Setare global built-ins — no import needed. But helper methods likeObject.groupBy()may need a polyfill. - Checked exceptions disappear: Java forces callers to handle checked exceptions. TypeScript has no mechanism for this. Document important error conditions in JSDoc comments.
- Java
equals()vs TS===: Java objects use.equals()for value comparison. TS===compares references for objects. Use deep-equal libraries or compare relevant fields explicitly. - Thread safety patterns are dead code: Remove all
synchronized,volatile,Lock,Atomic*patterns. JS is single-threaded. Keeping them adds confusion with zero benefit. - Builder pattern is often unnecessary: Java builders exist because constructors can't have named parameters. TypeScript objects with optional properties serve the same purpose more concisely.
- Over-engineering inheritance: Java projects often have deep class hierarchies. In TypeScript, prefer composition and interfaces. Flatten hierarchies where possible — if a class exists only to share one method, use a utility function instead.
- Lombok
@Dataon mutable classes: If the Java class was mutable (setters used), decide whether TS version should be mutable too. Often the answer is no — make propertiesreadonlyand create new instances instead of mutating. - Lombok
@Builder.Default: Default values in Lombok builders (@Builder.Default private boolean unfurlLinks = true) must become explicit defaults in the TS constructor destructuring:{ unfurlLinks = true }: Options. CompletableFuture.join()blocks the thread: There is NO blocking equivalent in JS.awaitis non-blocking. Code that usesjoin()for synchronous access must be redesigned to be fully async.ExecutorServicethread pools: Remove entirely. JS is single-threaded.Promise.all()provides concurrency for I/O-bound work without thread management. For CPU-bound work, use worker threads only if profiling shows a bottleneck.@FunctionalInterfacewith checked exceptions: Java functional interfaces can declarethrows Exception. TypeScript function types cannot. Async functions that reject should document their error types in JSDoc but cannot enforce catching at the type level.
references
- https://www.typescriptlang.org/docs/handbook/2/classes.html -- TS classes and inheritance
- https://www.typescriptlang.org/docs/handbook/2/generics.html -- TS generics
- https://www.typescriptlang.org/docs/handbook/decorators.html -- TS decorators (annotation equivalent)
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map -- JS Map (HashMap equivalent)
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise -- Promise (Future equivalent)
instructions
Use this expert when rewriting Java source code in TypeScript. Start by identifying the Java patterns in use (class hierarchies, generics, annotations, Lombok annotations, Stream API, CompletableFuture chains, functional interfaces, concurrency, Optional) and map each to its TS equivalent. Focus on simplification: flatten unnecessary class hierarchies, replace Lombok @Data/@Builder with interfaces and options objects, remove builder patterns in favor of typed options objects, rewrite CompletableFuture chains as async/await, convert @FunctionalInterface to type aliases, eliminate synchronization code, and convert getters/setters to direct property access. Pair with dependency-mapping-ts.md for Maven/Gradle → npm equivalents, type-mapping-ts.md for cross-language type reference, and json-serialization-ts.md for Gson/Jackson serialization conversion.
research
Deep Research prompt:
"Write a micro expert on converting Java to TypeScript. Cover: class hierarchies to interfaces/classes, generics mapping, annotations to decorators, Stream API to array methods, Optional to nullable types, checked exceptions removal, synchronized/volatile removal, enum with behavior to const objects, getter/setter simplification, builder pattern elimination, and package structure flattening. Include 3 worked examples."