TypeScript function return type based on input parameter

Arash Motamedi

I have a few different interfaces and objects that each have a type property. Let's say these are objects stored in a NoSQL db. How can I create a generic getItem function with a deterministic return type based on its input parameter type?

interface Circle {
    type: "circle";
    radius: number;
}

interface Square {
    type: "square";
    length: number;
}

const shapes: (Circle | Square)[] = [
    { type: "circle", radius: 1 },
    { type: "circle", radius: 2 },
    { type: "square", length: 10 }];

function getItems(type: "circle" | "square") {
    return shapes.filter(s => s.type == type);
    // Think of this as items coming from a database
    // I'd like the return type of this function to be
    // deterministic based on the `type` value provided as a parameter. 
}

const circles = getItems("circle");
for (const circle of circles) {
    console.log(circle.radius);
                       ^^^^^^
}

Property 'radius' does not exist on type 'Circle | Square'.

Arash Motamedi

Conditional Types to the rescue:

interface Circle {
    type: "circle";
    radius: number;
}

interface Square {
    type: "square";
    length: number;
}

type TypeName = "circle" | "square"; 

type ObjectType<T> = 
    T extends "circle" ? Circle :
    T extends "square" ? Square :
    never;

const shapes: (Circle | Square)[] = [
    { type: "circle", radius: 1 },
    { type: "circle", radius: 2 },
    { type: "square", length: 10 }];

function getItems<T extends TypeName>(type: T) : ObjectType<T>[]  {
    return shapes.filter(s => s.type == type) as ObjectType<T>[];
}

const circles = getItems("circle");
for (const circle of circles) {
    console.log(circle.radius);
}

Thanks Silvio for pointing me in the right direction.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS: Return function based on input type

From Dev

TypeScript return the type of parameter

From Dev

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

From Dev

Typescript: function return object type with field from parameter

From Dev

Typescript: function return object type with field from parameter

From Dev

Can typescript infer a function's response type based on parameter values?

From Dev

Typescript: Wrong overload selection based on function return type

From Java

Typescript return type depending on parameter

From Dev

How can I write a function have a polymorphic return type based on the type argument of its type parameter?

From Dev

Type for parameter in TypeScript Arrow Function

From Dev

Swift generics: return type based on parameter type

From Dev

Generic return type of function in Typescript

From Dev

return type for class function typescript

From Dev

Can we have a generic function with return type same as input parameter type?

From Dev

Typescript: How to infer a generic type in a higher order function from the input parameter of the returned function

From Dev

Return delegate function based on type

From Dev

Generics - Function return type the same as the parameter type?

From Dev

Return a new modified field object from a scala function that the input parameter is based on a trait

From Java

Execute method with a return type and input parameter in parallel

From Java

Infer return type of function based on type guard

From Dev

Is there a way to partially specialize my templated function based on a lambda parameter return type?

From Dev

meteor .helpers function input parameter to return an object

From Dev

Narrow return type based on input using generics

From Dev

Call nested function based on input parameter

From Dev

typescript arrow function parameter type safe

From Dev

typescript function parameter type unexpected token :

From Dev

function to return something based on input using R

From Dev

std::function with void return type and templated parameter

From Dev

Extension function asking for return type instead of parameter

Related Related

  1. 1

    AngularJS: Return function based on input type

  2. 2

    TypeScript return the type of parameter

  3. 3

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

  4. 4

    Typescript: function return object type with field from parameter

  5. 5

    Typescript: function return object type with field from parameter

  6. 6

    Can typescript infer a function's response type based on parameter values?

  7. 7

    Typescript: Wrong overload selection based on function return type

  8. 8

    Typescript return type depending on parameter

  9. 9

    How can I write a function have a polymorphic return type based on the type argument of its type parameter?

  10. 10

    Type for parameter in TypeScript Arrow Function

  11. 11

    Swift generics: return type based on parameter type

  12. 12

    Generic return type of function in Typescript

  13. 13

    return type for class function typescript

  14. 14

    Can we have a generic function with return type same as input parameter type?

  15. 15

    Typescript: How to infer a generic type in a higher order function from the input parameter of the returned function

  16. 16

    Return delegate function based on type

  17. 17

    Generics - Function return type the same as the parameter type?

  18. 18

    Return a new modified field object from a scala function that the input parameter is based on a trait

  19. 19

    Execute method with a return type and input parameter in parallel

  20. 20

    Infer return type of function based on type guard

  21. 21

    Is there a way to partially specialize my templated function based on a lambda parameter return type?

  22. 22

    meteor .helpers function input parameter to return an object

  23. 23

    Narrow return type based on input using generics

  24. 24

    Call nested function based on input parameter

  25. 25

    typescript arrow function parameter type safe

  26. 26

    typescript function parameter type unexpected token :

  27. 27

    function to return something based on input using R

  28. 28

    std::function with void return type and templated parameter

  29. 29

    Extension function asking for return type instead of parameter

HotTag

Archive