Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow non-identifiers to be used for JSXFragmentFactory #41400

Open
3 of 5 tasks
brainkim opened this issue Nov 4, 2020 · 2 comments
Open
3 of 5 tasks

Allow non-identifiers to be used for JSXFragmentFactory #41400

brainkim opened this issue Nov 4, 2020 · 2 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@brainkim
Copy link

brainkim commented Nov 4, 2020

Search Terms

JSX factory expression identifier string JSXFragmentFactory

Related Issues

#20469
#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.
@nojvek
Copy link
Contributor

nojvek commented Nov 4, 2020

@brainkim I'm one of the authors that landed non-react custom jsxFactory compiler option. I'm not sure what effort it would take to get "", but null totally works and has a test case in the repo.,

you can do jsxFragmentFactory: "null", and use that in your custom createElement to decide between a fragment and an actual tag.

It's used by snabbdom-jsx-lite https://github.com/nojvek/snabbdom-jsx-lite/blob/master/tsconfig.json#L8

@brainkim
Copy link
Author

brainkim commented Nov 4, 2020

@nojvek Ah I should have figured that null would have been supported; I just assumed it would be generalized to any literal value, and went ahead and made the Fragment tag the empty string.

The relevant PR is pretty informative about how this works, I’ll try to look through it soon.

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Nov 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants