Skip to content

Commit 2cbcee8

Browse files
Haroenvsarahdayan
andauthored
fix(types): complete used interface of JSX fallback (#16)
* ci(types): run type tests without react types as well * fix(types): fill in detailed fake interfaces for vdom * Apply suggestions from code review Co-authored-by: Sarah Dayan <[email protected]> * alias preact too (but don't run *preact tests) * fix: element itself shouldn't be generic * undo change * build deps too lol Co-authored-by: Sarah Dayan <[email protected]>
1 parent 1d03844 commit 2cbcee8

File tree

6 files changed

+111
-6
lines changed

6 files changed

+111
-6
lines changed

Diff for: .circleci/config.yml

+33-1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,37 @@ jobs:
107107
- run:
108108
name: Type checking
109109
command: yarn run test:types
110+
test_types_no_jsx_implementation:
111+
<<: *defaults
112+
steps:
113+
- checkout
114+
- run: *install_yarn_version
115+
- restore_cache: *restore_yarn_cache
116+
- run:
117+
name: Remove React types
118+
command: |
119+
# This simulates @types/react and preact not being installed
120+
node -e "
121+
fs.writeFileSync(
122+
'./package.json',
123+
JSON.stringify(
124+
Object.assign(require('./package.json'), {
125+
resolutions: {
126+
'@types/react': 'file:./scripts/empty-package',
127+
'preact': 'file:./scripts/empty-package',
128+
},
129+
}),
130+
null,
131+
2
132+
)
133+
);
134+
"
135+
# not running the scripts, as they should not run for *-react
136+
yarn install --ignore-scripts
137+
- run:
138+
name: Type checking
139+
# Using build:types instead of test:types, as it's easier to run only the VDOM types
140+
command: yarn build:types --scope '@algolia/*-{shared,vdom}'
110141
test_unit:
111142
<<: *defaults
112143
steps:
@@ -152,10 +183,11 @@ workflows:
152183
ci:
153184
jobs:
154185
- build
186+
- test_types
187+
- test_types_no_jsx_implementation
155188
- test_lint:
156189
requires:
157190
- build
158-
- test_types
159191
- test_unit:
160192
requires:
161193
- build

Diff for: packages/highlight-vdom/src/Highlight.tsx

+17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ import {
77
Renderer,
88
} from '@algolia/ui-components-shared';
99

10+
// Basic types to allow this file to compile without a JSX implementation.
11+
// This is a minimal subset of the actual types from the `JSX` namespace.
12+
interface IntrinsicElement extends JSX.IntrinsicAttributes {
13+
children?: ComponentChildren;
14+
className?: string;
15+
}
16+
17+
declare global {
18+
// eslint-disable-next-line @typescript-eslint/no-namespace
19+
namespace JSX {
20+
interface IntrinsicElements {
21+
span: IntrinsicElement;
22+
mark: IntrinsicElement;
23+
}
24+
}
25+
}
26+
1027
type HighlightPartProps = {
1128
children: ComponentChildren;
1229
classNames: Partial<HighlightClassNames>;

Diff for: packages/horizontal-slider-vdom/src/HorizontalSlider.tsx

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @jsx createElement */
2-
import { cx, Renderer } from '@algolia/ui-components-shared';
2+
import { ComponentChildren, cx, Renderer } from '@algolia/ui-components-shared';
33

44
import {
55
FrameworkProps,
@@ -8,6 +8,45 @@ import {
88
RecordWithObjectID,
99
} from './types';
1010

11+
// basic types to allow this file to compile without @types/react or preact
12+
// this is a minimal subset of the actual types, coming from the JSX namespace
13+
interface IntrinsicElement extends JSX.IntrinsicAttributes {
14+
children?: ComponentChildren;
15+
16+
className?: string;
17+
id?: string;
18+
tabIndex?: string | number;
19+
title?: string;
20+
21+
onClick?: (event: MouseEvent) => void;
22+
onKeyDown?: (event: KeyboardEvent) => void;
23+
onScroll?: (event: MouseEvent) => void;
24+
}
25+
26+
interface IntrinsicSvgElement extends IntrinsicElement {
27+
width?: string;
28+
height?: string;
29+
viewBox?: string;
30+
fill?: string;
31+
fillRule?: string;
32+
clipRule?: string;
33+
d?: string;
34+
}
35+
36+
declare global {
37+
// eslint-disable-next-line @typescript-eslint/no-namespace
38+
namespace JSX {
39+
interface IntrinsicElements {
40+
div: IntrinsicElement;
41+
button: IntrinsicElement;
42+
ol: IntrinsicElement;
43+
li: IntrinsicElement;
44+
svg: IntrinsicSvgElement;
45+
path: IntrinsicSvgElement;
46+
}
47+
}
48+
}
49+
1150
let lastHorizontalSliderId = 0;
1251

1352
export function generateHorizontalSliderId() {

Diff for: packages/shared/src/Renderer.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-empty-interface */
12
// Prevents type errors when using without a JSX implementation
23
// (e.g., Angular InstantSearch via InstantSearch.js)
34
// In the future, this may be fixable by accepting a JSX generic to every type
@@ -7,9 +8,16 @@
78
declare global {
89
// eslint-disable-next-line @typescript-eslint/no-namespace
910
namespace JSX {
10-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
11-
interface Element {}
12-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
11+
interface IntrinsicAttributes {
12+
key?: string | number | null;
13+
ref?: unknown;
14+
}
15+
16+
interface ElementChildrenAttribute {
17+
children: ComponentChildren;
18+
}
19+
20+
interface Element extends VNode {}
1321
interface IntrinsicElements {}
1422
}
1523
}
@@ -59,7 +67,10 @@ export type ComponentProps<
5967

6068
export type VNode<TProps = any> = {
6169
type: any;
62-
props: TProps & { children: ComponentChildren; key?: any };
70+
props: TProps & {
71+
children: ComponentChildren;
72+
key?: string | number | null;
73+
};
6374
};
6475

6576
export type Renderer = {

Diff for: scripts/empty-package/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// This file allows `empty-package` to be an `@types/` package

Diff for: scripts/empty-package/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "empty-package",
3+
"version": "0.0.0",
4+
"description": "An empty package used in tests as an alias for @types/"
5+
}

0 commit comments

Comments
 (0)