TypeScript return exact function infer type

Sam Chung

I have this function:

const plainText = (text: string) => ({
  type: 'plain_text',
  text,
  emoji: true,
});

At the moment the inferred type return is:

const plainText: (text: string) => {
    type: string;
    text: string;
    emoji: boolean;
}

Is there a way to make it return like this with the type of type being 'plain_text' as opposed to string? Or do I need to manually declare the return type?

const plainText: (text: string) => {
    type: 'plain_text';
    text: string;
    emoji: boolean;
}

I found I can do the following, but is this the best way to do it?

const plainText = (text: string) => ({
  type: 'plain_text' as 'plain_text',
  text,
  emoji: true,
});

I've got others like this:

const text = (text: string) => ({
  type: 'section',
  text: {
    type: 'mrkdwn',
    text,
  },
});

I'm trying to just create a bunch of functions to represent some repeated basic Slack Block types that I'm using so I can just pass them with eg. plainText('hello'). As I won't need to configure them, everything remains static and only the text changes.

The problem is when I call it against a Slack Client it required the typing to be exactly type: 'plain_text' instead of type: 'string'. Type definition

Linda Paiste

The problem is when I call it against a Slack Client it required the typing to be exactly type: 'plain_text' instead of type: 'string'. Type definition

You can declare a specific return type for your function rather than letting it be inferred. My recommendation is that you make use of the types which are already defined by Slack when you are making factories for them.

import {PlainTextElement} from "@slack/types";

const plainText = (text: string): PlainTextElement => ({
  type: 'plain_text',
  text,
  emoji: true,
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Infer return type of function based on type guard

From Dev

Why TypeScript can not infer type of recursive function

From Dev

infer template argument type from function return type

From Dev

How to infer the type of the function that can return type depending on condition?

From Dev

infer template argument type from function return type

From Dev

Is it possible to infer return type of functions in mapped types in typescript?

From Dev

Infer function generic type U from return value of passed function

From Dev

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

From Dev

How to infer return type of Promise based on function arguments?

From Dev

Generic return type of function in Typescript

From Dev

return type for class function typescript

From Dev

infer lambda return type in template

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

Implementing a function with conditional return type in Typescript

From Dev

Typescript throws error for return function 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

Why doesn't swift infer the appropriate overload function with a generic return argument without a type constraint?

From Dev

Why scala cannot infer common return type for a function with multiple parameter lists?

From Dev

Typescript - Function return type if I want to return object

From Dev

Typescript infer keyword inferring wrong type?

From Dev

TypeScript: Infer type of abstract method's implementation

From Dev

Typescript Infer type params with Compiler API

From Dev

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

From Dev

Minimal implementation of function with conditional type return type in TypeScript

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 Java

Unable to infer complex closure return type with SwiftUI

Related Related

  1. 1

    Infer return type of function based on type guard

  2. 2

    Why TypeScript can not infer type of recursive function

  3. 3

    infer template argument type from function return type

  4. 4

    How to infer the type of the function that can return type depending on condition?

  5. 5

    infer template argument type from function return type

  6. 6

    Is it possible to infer return type of functions in mapped types in typescript?

  7. 7

    Infer function generic type U from return value of passed function

  8. 8

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

  9. 9

    How to infer return type of Promise based on function arguments?

  10. 10

    Generic return type of function in Typescript

  11. 11

    return type for class function typescript

  12. 12

    infer lambda return type in template

  13. 13

    Specify return type in TypeScript arrow function

  14. 14

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

  15. 15

    TypeScript function return type based on input parameter

  16. 16

    Implementing a function with conditional return type in Typescript

  17. 17

    Typescript throws error for return function type

  18. 18

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

  19. 19

    Why doesn't swift infer the appropriate overload function with a generic return argument without a type constraint?

  20. 20

    Why scala cannot infer common return type for a function with multiple parameter lists?

  21. 21

    Typescript - Function return type if I want to return object

  22. 22

    Typescript infer keyword inferring wrong type?

  23. 23

    TypeScript: Infer type of abstract method's implementation

  24. 24

    Typescript Infer type params with Compiler API

  25. 25

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

  26. 26

    Minimal implementation of function with conditional type return type in TypeScript

  27. 27

    Typescript function return type depending on number or type of arguments

  28. 28

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

  29. 29

    Unable to infer complex closure return type with SwiftUI

HotTag

Archive