TS2378
TypeScript Programming Language
Severity: ModerateWhat Does This Error Mean?
TypeScript error TS2378 means you defined a getter property (get myProp()) in a class or object but the getter does not return a value in all code paths. Getters must always return a value — they describe a computed property, so calling them without returning something produces undefined unexpectedly. The fix is to add a return statement to every code path in the getter, or remove the getter and use a regular method or property instead.
Affected Models
- TypeScript 4.x
- TypeScript 5.x
- VS Code
- WebStorm
- Any TypeScript project
Common Causes
- The getter body has no return statement at all
- The getter has an if/else or switch statement but one branch does not return a value
- The getter returns void or calls a function that returns void instead of returning a value
- The getter was accidentally written as a setter (using an assignment instead of a return)
- A refactor removed the return statement but the getter declaration was left in place
How to Fix It
-
Locate the getter that is generating the error. Find the get keyword followed by the property name in your class or object literal. The getter body must have a return statement.
TypeScript error TS2378 includes the property name in the message, which helps identify exactly which getter needs attention.
-
Add a return statement to the getter body. A getter must return the value for the property. Example: get fullName(): string { return this.firstName + ' ' + this.lastName; }
The return type annotation after the getter name is optional but recommended. TypeScript infers the type from the return value.
-
If the getter has conditional logic, ensure all branches return a value. If any branch falls through without returning, TypeScript raises TS2378.
Example: get status() { if (this.active) { return 'active'; } return 'inactive'; } — both branches return, satisfying the requirement.
-
If the getter should sometimes return undefined, make the return type explicitly nullable. Example: get value(): string | undefined { if (!this.data) { return undefined; } return this.data.value; }
Explicitly returning undefined is valid and satisfies TS2378 — the error is about having no return, not about returning undefined.
-
If you do not need a getter at all, convert it to a regular property or method. A getter is appropriate when the value is computed dynamically. If the value is stored and does not need computation, a plain property is simpler.
Regular property: name: string = ''; — No getter needed. Getter: get formattedName(): string { return this.name.toUpperCase(); } — Computed on access.
When to Call a Professional
TS2378 is a TypeScript syntax and type error in your code. No external tools or services are needed. Adding a return statement to the getter resolves this error immediately.
Frequently Asked Questions
What is the difference between a getter and a regular property in TypeScript?
A regular property stores a value: name: string = 'John'. The value is stored in memory and read directly. A getter computes its value when accessed: get upperName(): string { return this.name.toUpperCase(); }. The computation runs every time the property is read. Getters look like properties from the outside (no () needed to call them) but execute code when accessed. Use getters for derived values that depend on other properties.
Can a getter return different types based on conditions?
Yes — the return type of a getter can be a union type if it returns different types in different branches. Example: get value(): string | number { if (this.isText) { return 'hello'; } return 42; } TypeScript infers the union type automatically, or you can specify it explicitly with a return type annotation. The important thing is that every code path returns something.
How do I pair a getter with a setter in TypeScript?
Define both get and set with the same property name in the same class. The getter provides the read behavior; the setter provides the write behavior. Example: get count(): number { return this._count; } and set count(value: number) { this._count = Math.max(0, value); } In TypeScript, if you have both a getter and setter, their types must be compatible — the setter's parameter type must be assignable to the getter's return type.