microsoft / microsoft/TypeScript

Allow non-identifiers to be used for JSXFragmentFactory

Open
#41,400 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Search Terms

JSX factory expression identifier string JSXFragmentFactory

Related Issues

https://github.com/Microsoft/TypeScript/issues/20469
https://github.com/microsoft/TypeScript/pull/38720

Suggestion

Allow non-identifiers to be used as the JSXFragmentFactory. Currently, attempting to use a non-identifier like the empty string results in the following error:

  tsconfig.json(9,27): error TS18035: Invalid value for 'jsxFragmentFactory'. '''' is not a valid identifier or qualified-name.

My config:

{
  "include": ["src", "types"],
  "compilerOptions": {
    "module": "esnext",
    "target": "esnext",
    "moduleResolution": "node",
    "jsx": "preserve",
    "jsxFactory": "createElement",
    "jsxFragmentFactory": "''",
    "lib": ["esnext", "dom"],
    "baseUrl": "./",
    /* paths - If you configure Snowpack import aliases, add them here. */
    "paths": {},
    /* noEmit - Snowpack builds (emits) files, not tsc. */
    "noEmit": true,
    /* Additional Options */
    "strict": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports": true,
    "importsNotUsedAsValues": "error"
  }
}

Use Cases

My JSX framework defines the Fragment tag as the empty string. Babel’s JSX support allows non-identifiers to be used as the JSXFragmentFactory so I wonder why we can’t do the same in TypeScript.

Using an identifier is a little more awkward.

Examples

With the jsxFactory set to createElement and the jsxFragmentFactory set to the empty string, emitted code might look like the following.

createElement(
  "",
  null,
  createElement("div", null),
  createElement("div", null),
);

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the jsxFragmentFactory compiler option validation and the related issue and pull request linked in the report. Done means non-identifier values such as the empty string are accepted and JSX fragments emit using the configured factory without the current invalid-identifier error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.