Have you ever encountered the error "Function call on left hand side of assignment must return variant or object" in TypeScript? This issue often crops up when you're trying to assign a value to a variable, but the function you're calling on the left-hand side doesn't return a value that TypeScript can infer as a variant or object. Let's dive into this error, understand its causes, and learn how to fix it.
Understanding the Error
The error message is quite descriptive. It's telling you that the function you're calling in an assignment must return a value that can be treated as a variant (a union type) or an object. This is because TypeScript infers the type of the variable you're assigning to based on the return type of the function.
Example: The Error in Action
Let's consider a simple example:

let result = someFunction();
If `someFunction` doesn't return any value (i.e., it's of type `void`), TypeScript won't know what type to infer for `result`. This is where the error occurs.
Causes of the Error
Here are a few scenarios that might trigger this error:
- Missing Return Statement: You forgot to include a return statement in your function, or the function doesn't return any value.
- Incorrect Return Type: The function returns a value, but it's not a variant or object. For example, if the function returns a primitive type like `number` or `string`, this error might occur.
- Async Functions: If you're using an async function, ensure that it's returning a Promise that resolves to a variant or object.
Example: Incorrect Return Type
Here's an example where the function returns a primitive type:

function getName(): number {
return 1;
}
let name: string = getName(); // Error: Type 'number' is not assignable to type 'string'.
Solving the Error
The solution to this error usually involves ensuring that the function you're calling returns a value that TypeScript can infer as a variant or object. Here's how you can do it:
- Add a Return Statement: If your function doesn't have a return statement, add one. If it does, ensure that it's returning a value.
- Change the Return Type: If the function returns a primitive type, change the return type to a variant or object. You can use TypeScript's union types to achieve this.
- Handle Async Functions: If you're using an async function, ensure that it's returning a Promise that resolves to a variant or object.
Example: Fixing the Incorrect Return Type
Here's how you can fix the previous example:
function getName(): string | null {
return null;
}
let name: string = getName(); // No error
Best Practices
To avoid this error in the future, consider the following best practices:

- Always include a return statement in your functions.
- Be mindful of the return types of your functions. If you're not sure, consider using union types to make the return type more flexible.
- When working with async functions, ensure that the Promise resolves to a variant or object.
By following these best practices, you can minimize the chances of encountering this error in your TypeScript code.





















