Implementing a function with conditional return type in Typescript

arslancharyev31

Let us say I want to write a function that can accept a single argument which can be either of nullable or non-nullable type. If the argument is non-nullable, then the return type should also be non-nullable. Similarly, if the argument is of nullable type, so too should be the return type.

Below is my attempt at implementing such function:

type TransformKey = 'one' | 'two' | 'three'
function transform<T extends TransformKey | null>(key: T): T extends TransformKey ? string : string | null {
    if (key === null)
        return null as any
    else
        return key.toUpperCase() as any
}

The function signature appears to be correct, since it achieves the desired behavior on call-site, for example:

const neverNullArg = 'two'
const neverNullResult: string = transform(neverNullArg)

const maybeNullArg: TransformKey | null = 'three'
const maybeNullResult: string | null = transform(maybeNullArg)

i.e. the return type is indeed determined by the argument type in a correct manner.

However, my issue lies with the function implementation. Without casting the return value to any, both return statements result in TS2322 error: Type 'null'/'string' is not assignable to type 'T extends TransformKey ? string : string | null'. So I was wondering how could the function be implemented in a way that satisfies the signature without defeating the typing system using as any casts or // @ts-ignore

smac89

You can create overloads for your function:

type TransformKey = 'one' | 'two' | 'three'

function transform(key: TransformKey): string;
function transform(key: TransformKey | null): string | null;
function transform(key: any): string | null {
    if (key === null)
        return null;
    else
        return key.toUpperCase();
}

Tests

const neverNullArg = 'two'
const neverNullResult: string = transform(neverNullArg)

const maybeNullArg: TransformKey | null = 'three'
const maybeNullResult: string | null = transform(maybeNullArg)

See also: Writing Good Overloads

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Minimal implementation of function with conditional type return type in TypeScript

From Dev

Generic return type of function in Typescript

From Dev

return type for class function typescript

From Dev

Typescript function output cannot be assigned to conditional type

From Dev

Return type issue in generic function using conditional type

From Dev

Can I use conditional generic to set a callback return type in typescript?

From Java

Specify return type in TypeScript arrow function

From Java

TypeScript: Is it possible to get the return type of a generic function?

From Java

TypeScript function return type based on input parameter

From Dev

TypeScript return exact function infer type

From Dev

Typescript throws error for return function type

From Dev

Typescript - Function return type if I want to return object

From Dev

Typescript: Constrain function generic type based on the expected return type

From Dev

Typescript function return type depending on number or type of arguments

From Dev

TypeScript: How to type a returned function's return type

From Dev

swift conditional function return

From Dev

Conditional return in a PHP function

From Dev

How can I define a return type of void for a function in a Typescript interface?

From Dev

Is it possible in typescript to have a function return the type of a class that extends it

From Java

Typescript | Warning about Missing Return Type of function, ESLint

From Dev

How can I define the return type of a lodash reduce function with Typescript?

From Dev

Typescript: function return object type with field from parameter

From Dev

Why isn't Typescript requiring my function to return a certain type?

From Dev

Typescript: function return object type with field from parameter

From Dev

How can I define the return type of a lodash reduce function with Typescript?

From Dev

typescript function declared a non-void type but has no return expression

From Dev

Typescript error when function return type is Promise<{ then: () => void }>

From Dev

Typescript: Wrong overload selection based on function return type

From Dev

Typescript - Make the function return type the class that an interface implements

Related Related

  1. 1

    Minimal implementation of function with conditional type return type in TypeScript

  2. 2

    Generic return type of function in Typescript

  3. 3

    return type for class function typescript

  4. 4

    Typescript function output cannot be assigned to conditional type

  5. 5

    Return type issue in generic function using conditional type

  6. 6

    Can I use conditional generic to set a callback return type in typescript?

  7. 7

    Specify return type in TypeScript arrow function

  8. 8

    TypeScript: Is it possible to get the return type of a generic function?

  9. 9

    TypeScript function return type based on input parameter

  10. 10

    TypeScript return exact function infer type

  11. 11

    Typescript throws error for return function type

  12. 12

    Typescript - Function return type if I want to return object

  13. 13

    Typescript: Constrain function generic type based on the expected return type

  14. 14

    Typescript function return type depending on number or type of arguments

  15. 15

    TypeScript: How to type a returned function's return type

  16. 16

    swift conditional function return

  17. 17

    Conditional return in a PHP function

  18. 18

    How can I define a return type of void for a function in a Typescript interface?

  19. 19

    Is it possible in typescript to have a function return the type of a class that extends it

  20. 20

    Typescript | Warning about Missing Return Type of function, ESLint

  21. 21

    How can I define the return type of a lodash reduce function with Typescript?

  22. 22

    Typescript: function return object type with field from parameter

  23. 23

    Why isn't Typescript requiring my function to return a certain type?

  24. 24

    Typescript: function return object type with field from parameter

  25. 25

    How can I define the return type of a lodash reduce function with Typescript?

  26. 26

    typescript function declared a non-void type but has no return expression

  27. 27

    Typescript error when function return type is Promise<{ then: () => void }>

  28. 28

    Typescript: Wrong overload selection based on function return type

  29. 29

    Typescript - Make the function return type the class that an interface implements

HotTag

Archive