Skip to content

TS 4.0: jsxFragmentFactory ignored in Preact + TypeScript setup #39451

@ddprrt

Description

@ddprrt

I'm using the new jsxFragmentFactory to make Fragment work in a Preact setup, but TypeScript won't recognize it.

TypeScript Versions: 4.0.0-beta, 4.0.0-dev.20200706

Search Terms: JSX Fragment Factory, Preact

Code

Counter.tsx

import { Fragment, h } from "preact"

import { useState } from "preact/hooks"

type CounterProps = {
  count?: number
}

export function Counter({count = 0}: CounterProps) {
  const [cnt, setCnt] = useState(count)
  return <>
    <p>{cnt}</p>
    <button onClick={() => setCnt((prev) => prev + 1)} type="button">Update</button>
  </>
}

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "lib": ["DOM", "DOM.Iterable", "esnext"],
    "allowJs": false,
    "skipLibCheck": false,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "preserve",
    "jsxFactory": "h",
    "jsxFragmentFactory": "Fragment",
    "noUnusedLocals": true
  },
  "include": ["src"]
}

See also:

Screenshot 2020-07-06 at 12 55 33

Expected behavior: TypeScript recognizes that I use <>...</> as a shorthand for the imported Fragment

Actual behavior:
With "noUnusedLocals" set to true, TypeScript throws error TS6133: 'Fragment' is declared but its value is never read.

Playground Link: No playground, but a GitHub repo (https://github.com/ddprrt/preact-vite-ts-playground) -- this is a Vite setup that compiles, but I think this can be ignored for this issue. Editor feedback and running tsc show the actual behavior.

Hope this helps!

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: JSX/TSXRelates to the JSX parser and emitterFix AvailableA PR has been opened for this issueGood First IssueWell scoped, documented and has the green light

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions