Skip to content

TypeScript Compiler compiles Generic ArrowFunction into a TsxElementΒ #60474

Closed as not planned

Description

πŸ”Ž Search Terms

  • generic
  • array
  • function
  • compile
  • tsxelement

πŸ•— Version & Regression Information

  • This is the behavior in typescript@next version I tried

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.0-dev.20241110#code/MYewdgzgLgBA5gUygKQMoHkByMC8MA8AKgHwAUoYAZgJZwBcMA3gFAwwCuATgDYPSfUwcADSsYACwQBDACYJOEAPwNGMANoBrBAE8+UAUIC6eg3BgBfUeYCUDAAqcQAW2oQERYrk8s2FaDEokYHEAYXAaMzxSHzYYJyRxEBkGAHIAcQBRQhTRWJgUgEFgYAQABygU1KlS0u5qYCkoanAAegArCHAcsTYUsLAoBAGAWkJtUoRK-Ora+sbmsHbOsG68gDoN8nDaNclZeQgYAB8jphsxGwBuMU4kLjAAoPEtqh2ubmFHqGD+iOsemBrKCSMAeUi3CClcBuLwwCFQyAINYdcCkazWa7mS5AA

πŸ’» Code

ArrayFunction is compatible with Generics, is there a bug in TypeScript Compiler or is the usage introduced in TypeScript Deep Dive incorrect?

The following compilers compiled to ArrayFunction:

  • babel-eslint9
  • @babel/parser
  • esformatter
  • flow
  • hermes-parser

Use the following code from the TypeScript Deep Dive:

const getJSON = <T>(config: {
  url: string,
  headers?: { [key: string]: string },
}): Promise<T> => {
  const fetchConfig = ({
    method: 'GET',
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    ...(config.headers || {})
  });
  return fetch(config.url, fetchConfig)
    .then<T>(response => response.json());
};

πŸ™ Actual behavior

Result transpiled to JavaScript:

"use strict";
const getJSON = React.createElement(T, null,
    "(config: ",
    url,
    " string, headers?: ",
    [key],
    " string]: string }, }): Promise",
    React.createElement(T, null,
        " => ",
    ,
        " fetchConfig = (",
        method,
        " 'GET', 'Accept': 'application/json', 'Content-Type': 'application/json', ...(config.headers || ",
        ") }); return fetch(config.url, fetchConfig) .then",
        React.createElement(T, null, "(response => response.json()); };")));

πŸ™‚ Expected behavior

"use strict";
const getJSON = (config) => {
    const fetchConfig = (Object.assign({ method: 'GET', 'Accept': 'application/json', 'Content-Type': 'application/json' }, (config.headers || {})));
    return fetch(config.url, fetchConfig)
        .then(response => response.json());
};

Additional information about the issue

No response

Activity

  1. MartinJohns commented on Nov 11, 2024

    @MartinJohns
    Contributor

    This is working as intended / a design limitation due to the ambigious syntax for jsx elements and generics. You either need to disable jsx in your config, or use the workaround syntax <T,> instead of <T> for your generic.

  2. typescript-bot commented on Nov 15, 2024

    @typescript-bot
    Contributor

    This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  3. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions