Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/react17-jsx-typing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@linaria/react': patch
---

Fix TypeScript typings for React 17 projects using the automatic JSX runtime (`jsx: react-jsx`), so `styled.*` intrinsic components don’t incorrectly require `children`.
1 change: 1 addition & 0 deletions packages/react/__dtslint-react17__/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// dtslint expects an `index.d.ts` in the test folder.
8 changes: 8 additions & 0 deletions packages/react/__dtslint-react17__/react17.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { styled } from '..';

const Button = styled.button``;

// Should not require `children` for intrinsic elements on React 17 projects.
Button({});
<Button />;
<Button>ok</Button>;
17 changes: 17 additions & 0 deletions packages/react/__dtslint-react17__/stubs/jsx-runtime.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
declare module 'react/jsx-runtime' {
const Fragment: unknown;
function jsx(type: unknown, props: unknown, key?: unknown): unknown;
function jsxs(type: unknown, props: unknown, key?: unknown): unknown;
}

declare module 'react/jsx-dev-runtime' {
const Fragment: unknown;
function jsxDEV(
type: unknown,
props: unknown,
key: unknown,
isStaticChildren: boolean,
source: unknown,
self: unknown
): unknown;
}
27 changes: 27 additions & 0 deletions packages/react/__dtslint-react17__/stubs/react17.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Minimal React 17-like type surface for dtslint.
// It intentionally does NOT export a module-level `JSX` namespace.

declare namespace React {
type ElementType = unknown;

interface CSSProperties {
[key: string]: unknown;
}

interface FunctionComponent<P = Record<string, unknown>> {
(props: P & { children?: unknown }): unknown;
}

function createElement(...args: unknown[]): unknown;
}

export = React;
export as namespace React;

declare global {
namespace JSX {
interface IntrinsicElements {
button: Record<string, unknown>;
}
}
}
3 changes: 3 additions & 0 deletions packages/react/__dtslint-react17__/tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./tsconfig.json"
}
22 changes: 22 additions & 0 deletions packages/react/__dtslint-react17__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"target": "ES2015",
"jsx": "react-jsx",
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"types": [],
"noEmit": true,
"baseUrl": "./",
"paths": {
"react": ["./stubs/react17.d.ts"],
"react/jsx-runtime": ["./stubs/jsx-runtime.d.ts"],
"react/jsx-dev-runtime": ["./stubs/jsx-runtime.d.ts"]
}
}
}
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"build:declarations": "tsc --emitDeclarationOnly --outDir types",
"build:dist": "tsup --format cjs,esm",
"test": "jest --config ../../jest.config.js --rootDir .",
"test:dts": "dtslint --localTs ../../node_modules/typescript/lib __dtslint__",
"test:dts": "dtslint --localTs ../../node_modules/typescript/lib __dtslint__ && dtslint --localTs ../../node_modules/typescript/lib __dtslint-react17__",
"typecheck": "tsc --noEmit --composite false",
"watch": "pnpm build:dist --watch & pnpm build:declarations --watch"
},
Expand Down
20 changes: 10 additions & 10 deletions packages/react/src/styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,25 @@ interface IProps {
style?: Record<string, string>;
}

// React <19
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace JSX {
interface IntrinsicElements {}
}
}

// React >=19
declare module 'react' {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace JSX {
interface IntrinsicElements {}
}
}

let idx = 0;

type IntrinsicElements = React.JSX.IntrinsicElements & JSX.IntrinsicElements;
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
type ReactIntrinsicElements = typeof import('react') extends {
JSX: { IntrinsicElements: infer T };
}
? T
: never;

type IntrinsicElements = [ReactIntrinsicElements] extends [never]
? JSX.IntrinsicElements
: ReactIntrinsicElements;

// Components with props are not allowed
function styled(
Expand Down
1 change: 1 addition & 0 deletions packages/react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"node"
],
},
"include": ["src"],
"references": [{ "path": "../core" }]
}