Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
Narrow down primitive return type of function at call site in Typescript

Writing a function that can take an any value and returns only boolean or number in typescript, it all works fine, until we need to narrow down the return value of the function at the call site to suit some variable type(or parameter type in case of functions compositions)

Here's the function

function parseValue(value: any, defaultValue: number | boolean): number | boolean {

    const valueType = typeof value;


    switch (valueType) {
        case 'undefined': {
            return defaultValue;
        }

        case 'boolean': {
            return value;
        }

        default: {

            if (value == 'Unlimited')
                return Infinity;

            return parseInt(value);
        }
    }

}

The problem is seen at call site

let booleanResult = parseValue(true, true);

// Error: type boolean | number is not assignable to type boolean
let booleanCondition: boolean = booleanResult;


let numberResult = parseValue(2, 1);

// Error: type boolean | number is not assignable to type number
let numberValue: number = numberResult;

Is there a way in typescript which allow us to specify the return type at call site?


Overloading is not compatible! It looks like I can't make on overload a function with different param type(correct me if I'm wrong here)

type returnType = boolean | number;

// This overload signature is not compatible with its implementation signature

function parseValue(value: any, defaultValue:  boolean):  returnType;
function parseValue(value: any, defaultValue: number ): returnType{
 ...
}

I know we can parse the output of the function again to make it works like:

let numberValue: number = Number(numberResult);

But I'm looking for a typescript solution for this problem.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use typescript overload the proper way:

From Typescript docs

The signature of the implementation is not visible from the outside. When writing an overloaded function, you should always have two or more signatures above the implementation of the function.

The solution was easily done writing each overload function alone, then writing the body of the function

function parseValue(value: any, defaultValue: number): number;
function parseValue(value: any, defaultValue: boolean): boolean;
function parseValue(value: any, defaultValue: number | boolean): boolean | number {
 ... body of function
}

My problem was solved when I've read

Again, the signature used to write the function body can’t be “seen” from the outside.

So I was kind of merging the overload with the implementation, where we should have separated them

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!