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 Java

Specify return type in TypeScript arrow function

From Java

Unable to infer complex closure return type with SwiftUI

From Java

TypeScript function return type based on input parameter

From Java

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

From Dev

infer lambda return type in template

From Dev

return type for class function typescript

From Dev

infer template argument type from function return type

From Dev

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

From Dev

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

From Dev

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

From Dev

Generic return type of function in Typescript

From Dev

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

From Dev

Minimal implementation of function with conditional type return type in TypeScript

From Dev

Typescript infer keyword inferring wrong type?

From Dev

Infer function generic type U from return value of passed function

From Dev

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

From Dev

Typescript - Function return type if I want to return object

From Dev

Implementing a function with conditional return type in Typescript

From Dev

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

From Dev

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

From Dev

TypeScript: Infer type of abstract method's implementation

From Dev

Typescript throws error for return function type

From Dev

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

From Dev

Typescript Infer type params with Compiler API

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

infer template argument type from function return type

From Dev

Why TypeScript can not infer type of recursive function

Related Related

  1. 1

    Infer return type of function based on type guard

  2. 2

    Specify return type in TypeScript arrow function

  3. 3

    Unable to infer complex closure return type with SwiftUI

  4. 4

    TypeScript function return type based on input parameter

  5. 5

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

  6. 6

    infer lambda return type in template

  7. 7

    return type for class function typescript

  8. 8

    infer template argument type from function return type

  9. 9

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

  10. 10

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

  11. 11

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

  12. 12

    Generic return type of function in Typescript

  13. 13

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

  14. 14

    Minimal implementation of function with conditional type return type in TypeScript

  15. 15

    Typescript infer keyword inferring wrong type?

  16. 16

    Infer function generic type U from return value of passed function

  17. 17

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

  18. 18

    Typescript - Function return type if I want to return object

  19. 19

    Implementing a function with conditional return type in Typescript

  20. 20

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

  21. 21

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

  22. 22

    TypeScript: Infer type of abstract method's implementation

  23. 23

    Typescript throws error for return function type

  24. 24

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

  25. 25

    Typescript Infer type params with Compiler API

  26. 26

    Typescript function return type depending on number or type of arguments

  27. 27

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

  28. 28

    infer template argument type from function return type

  29. 29

    Why TypeScript can not infer type of recursive function

HotTag

Archive